﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Data.SqlClient;
using System.Diagnostics;
using System.EnterpriseServices;
using System.Linq;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using BLL;
using Common;
using Model;
using System.Text;
using System.Web;
using System.Net;
using System.Data.OleDb;
using System.Collections;
using System.Data.OracleClient;
using Npgsql;

namespace WebPortal.Controllers
{
    [Transaction(TransactionOption.Required)]
    public class SystemConfigurationController : BaseController
    {
        #region 常量

        private const string SQLSERVER_FRAMAT = "Server={0};database={1};uid={2};pwd={3};";
        private const string ORACLE_FRAMAT = @"Data Source=(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = {0})(PORT = {4}))(CONNECT_DATA =(SID = {5})(SERVER = DEDICATED)(SERVICE_NAME = {1})));User Id={2};Password={3};";
        private const string ACCESS_FRAMAT = "{0}|{1}|{2}";
        private const string MYSQL_FRAMAT = "Server={0};port={4};database={1};uid={2};pwd={3};";
        private const string EXCEL_FRAMAT = ACCESS_FRAMAT;
        private const string TXT_FRAMAT = ACCESS_FRAMAT;

        #endregion

        private static EncrypCode encrypCode = new EncrypCode();

        #region 源系统信息
        public ActionResult EtlSourceList(string dbName)
        {
            PageRole role = new PageRole();
            role.role = SysPublicBLL.GetUserRole(Request.Cookies["MDM_UserName"].Value, dbName);
            return View(role);
        }
        public ActionResult AddDB()
        {
            return View();
        }
        public ActionResult AddCallBack()
        {
            return View();
        }
        public ActionResult AddWDC()
        {
            return View();
        }
        public ActionResult GetPath()
        {
            return View();
        }
        public ActionResult UserList()
        {
            return View();
        }
        /// <summary>
        /// 源系统列表
        /// </summary>
        /// <param name="name">系统名称</param>
        /// <param name="sourceType">系统类型</param>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult EtlSourceList(string dbName, string name, string sourceType, string contact, int pageSize = 10, int pageIndex = 1)
        {
            int sourceTypeVal = string.IsNullOrEmpty(sourceType) ? 0 : int.Parse(sourceType);
            pageSize = int.Parse(Request.Params["rows"]);
            pageIndex = int.Parse(Request.Params["page"]);

            string order = Request.Params["order"];
            string sort = Request.Params["sort"];
            if (string.IsNullOrEmpty(sort))
                sort = "CreateTime";
            string baseDB = System.Configuration.ConfigurationManager.AppSettings["DataBase"];
            SqlParameter[] sqlparams = new SqlParameter[]
            {
                new SqlParameter("@SourceSysName", name),
                new SqlParameter("@SourceType",sourceTypeVal),
                new SqlParameter("@Contact", contact),
                new SqlParameter("@baseDB", baseDB),
                new SqlParameter("@Order", order),
                new SqlParameter("@Sort", sort),
                new SqlParameter("@Rows", pageSize),
                new SqlParameter("@PageNum", pageIndex)
            };
            LoginInfo loginInfos = base.loginInfo();
            loginInfos.DBName = dbName;
            EtlSourceSystemBLL bll = new EtlSourceSystemBLL(loginInfos);
            IList<EtlSourceSystemInfo> list = bll.GetList(sqlparams);//得到数据列表 
            int count = 0;
            int count2 = 0;
            if (list.Count > 0)
                count = list[0].Count;
            EtlSourceViewConfigBLL sourceViewConfigBLL = new EtlSourceViewConfigBLL(loginInfos);
            foreach (EtlSourceSystemInfo model in list)
            {
                model.CreateTime = Convert.ToDateTime(model.CreateTime).ToString("yyyy-MM-dd");
                if (model.StatusStr == "1")
                    model.StatusStr = "启用";
                else
                    model.StatusStr = "<font color='#ff0000'>禁用</font>";
                List<ETL_Source_ViewConfigInfo> viewList = sourceViewConfigBLL.GetViewConfigById(model.GUID, "CreateTime", "desc", 1, 100, out count2);
                int stepStatus = 0;
                if (viewList.Count > 0)
                    stepStatus = 2;
                foreach (ETL_Source_ViewConfigInfo viewModel in viewList)
                {
                    ETL_Task_List_History etlStepModel = sourceViewConfigBLL.GetStepStatus(viewModel.Guid);
                    if (etlStepModel.TaskStatus != 2)
                    {
                        stepStatus = etlStepModel.TaskStatus;
                        break;
                    }
                }
                model.ETLStatus = stepStatus;
            }
            Dictionary<String, Object> jsonDict = new Dictionary<String, Object>();
            jsonDict.Add("total", count);
            jsonDict.Add("rows", list);
            return Json(jsonDict);
        }
        /// <summary>
        /// 编辑源系统信息
        /// </summary>
        /// <param name="systemID"></param>
        /// <param name="dbName"></param>
        /// <returns></returns>
        public ActionResult EtlSourceAdd(string systemID, string dbName)
        {
            LoginInfo loginInfos = base.loginInfo();
            loginInfos.DBName = dbName;
            EtlSourceSystemBLL bll = new EtlSourceSystemBLL(loginInfos);
            EtlSourceSystemInfo model = new EtlSourceSystemInfo();
            if (string.IsNullOrEmpty(systemID))
                model.SourceType = 1;
            else
                model = bll.GetEtlSourceSystemInfoById(systemID);
            return View(model);
        }
        /// <summary>
        /// 保存源系统信息
        /// </summary>
        /// <param name="data">源系统信息</param>
        /// <param name="dbConfigs">连接字符串</param>
        /// <returns></returns>
        public string EtlSourceUpdate(EtlSourceSystemInfo data, FormCollection collection, string dbName)
        {
            string DBType = Request.Params["DBType"];
            LoginInfo loginInfos = base.loginInfo();
            loginInfos.DBName = dbName;
            EtlSourceSystemBLL sourcesystemBll = new EtlSourceSystemBLL(loginInfos);
            var par = new SqlParameter[] { new SqlParameter("@SourceSysName", data.SourceSysName) };
            string result = "";
            string[] msg = { };
            try
            {
                if (string.IsNullOrEmpty(data.GUID) && sourcesystemBll.GetSourceSystemByName(par))
                {
                    return "2";
                }
                JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
                IList<ETLCallBackConfig> cbList = (IList<ETLCallBackConfig>)jsSerializer.Deserialize(collection["CBs"], typeof(IList<ETLCallBackConfig>));
                #region 检查HTTP回调的地址是否正常

                //foreach (ETLCallBackConfig cbConfig in cbList)
                //{
                //    if (cbConfig.CallBackType.Equals(2))
                //    {
                //        string targetURL = cbConfig.CallBackName;
                //        Hashtable param = new Hashtable();
                //        string para = cbConfig.Paras;
                //        string paraValues = cbConfig.ParaValues;
                //        if (!string.IsNullOrEmpty(para))
                //        {
                //            List<string> paraArr = para.Split('&').ToList();
                //            List<string> paraValuesArr = paraValues.Split('&').ToList();
                //            for (int i = 0; i < paraArr.Count; i++)
                //            {
                //                //添加回调配置参数
                //                param[paraArr[i]] = paraValuesArr[i];
                //            }
                //        }
                //        string meghod = "POST";
                //        var encoding = Encoding.UTF8;
                //        var errorMessage = string.Empty;
                //        var callResult = HttpUtils.IsExist(targetURL, param, meghod, encoding, (e) =>
                //        {
                //            return true;
                //        }, (e) =>
                //        {
                //            var we = e as System.Net.WebException;
                //            if (we != null)
                //            {
                //                var response = we.Response as System.Net.HttpWebResponse;
                //                if (response != null)
                //                {
                //                    if (response.StatusCode == HttpStatusCode.NotFound)
                //                    {
                //                        errorMessage = e.Message;

                //                        return false;
                //                    }
                //                }
                //            }
                //            //errorMessage = e.Message;
                //            return true;
                //        });
                //        if (callResult == false)
                //        {
                //            return string.Format("回调地址 {0} 调用错误：{1}", targetURL, errorMessage);
                //        }
                //    }
                //}

                #endregion

                if (string.IsNullOrEmpty(data.GUID))//新增源系统
                {
                    data.GUID = Guid.NewGuid().ToString();

                    result = sourcesystemBll.InsertWithExist(data).ToString();
                    new SysOperationLog(loginInfos).Log("源系统管理", Request.UserHostAddress, OperationAction.添加, OperationStatus.成功, "添加源系统信息操作,操作成功,ID=" + data.GUID, Request.Cookies["MDM_UserName"] != null ? Request.Cookies["MDM_UserName"].Value : "");

                }
                else//保存源系统
                {
                    data.DBType = DBType;
                    result = sourcesystemBll.Update2(data).ToString();
                    new SysOperationLog(loginInfos).Log("源系统管理", Request.UserHostAddress, OperationAction.更新, OperationStatus.成功, "更新源系统信息操作,操作成功,ID=" + data.GUID, Request.Cookies["MDM_UserName"] != null ? Request.Cookies["MDM_UserName"].Value : "");
                }
                if (result == "1")//保存成功，保存连接字符串和联系人
                {

                    #region 更新连接字符串
                    //已保存数据库连接串
                    IList<ETLDbConfig> oldDBlist = sourcesystemBll.GetETLDbConfigList(new IDataParameter[] { new SqlParameter("@SysID", data.GUID) });

                    //提交的数据库连接串
                    List<ETLDbConfig> conList = GetDatabaseConnections(collection);
                    foreach (ETLDbConfig etlDBInfo in oldDBlist)//删除移除的连接
                    {
                        if (conList.FindIndex(o => { return o.GUID == etlDBInfo.GUID; }) == -1)
                        {
                            sourcesystemBll.DeleteDbConfig(etlDBInfo);
                        }
                    }
                    for (int i = 0; i < conList.Count; i++)
                    {
                        ETLDbConfig dbConfigInfo = conList[i];
                        dbConfigInfo.DbSortID = i;
                        dbConfigInfo.SysID = data.GUID;
                        dbConfigInfo.DbType = data.SourceType;
                        dbConfigInfo.CreateUser = data.CreateUser ?? loginInfos.Dbuser;

                        //ETLDbConfig oldDBConfig = new ETLDbConfig();
                        //if (oldDBlist.Count > 0)
                        //    oldDBConfig = oldDBlist.First(o => { return o.DbDataSource == dbConfigInfo.DbDataSource; });
                        if (dbConfigInfo.GUID.Length == 36)
                        {
                            sourcesystemBll.UpdateDbConfig(dbConfigInfo);
                        }
                        else
                        {
                            dbConfigInfo.GUID = Guid.NewGuid().ToString();
                            sourcesystemBll.AddDbConfig(dbConfigInfo);
                            if (DBType == "2")//大数据更新标识配置表
                            {
                                var viewLists = sourcesystemBll.GetSourceViewConfigBySystemID(data.GUID);
                                if (viewLists.Count > 0)
                                {
                                    sourcesystemBll.UpdateBigDateTagConfig(data.GUID, dbConfigInfo.GUID);
                                }
                            }
                        }
                    }


                    #endregion
                    #region 通配符配置
                    List<ETLWDCConfig> wdcList = (List<ETLWDCConfig>)jsSerializer.Deserialize(collection["WDCs"], typeof(List<ETLWDCConfig>));//页面传值
                    IList<ETLWDCConfig> list = sourcesystemBll.GetWDCConfigList(new IDataParameter[] { new SqlParameter("@SysID", data.GUID) });//保存的记录
                    foreach (ETLWDCConfig item in list)//删除记录
                    {
                        if (wdcList.FindIndex(o => { return o.GUID == item.GUID; }) == -1)
                        {
                            sourcesystemBll.DeleteWDCConfig(item.GUID);
                        }
                    }
                    foreach (ETLWDCConfig wdcInfo in wdcList)
                    {
                        wdcInfo.SysID = data.GUID;
                        if (string.IsNullOrEmpty(wdcInfo.GUID))//添加记录
                        {
                            wdcInfo.GUID = Guid.NewGuid().ToString();
                            sourcesystemBll.AddWDCConfig(wdcInfo);
                        }
                        else
                            sourcesystemBll.UpdateWDCConfig(wdcInfo);//更新记录
                    }

                    #endregion
                    #region 回调配置
                    //删除旧记录,通配符,回调配置,联系人信息
                    if (sourcesystemBll.DelSysConfig(data.GUID))
                    {
                        foreach (ETLCallBackConfig cbConfig in cbList)
                        {
                            cbConfig.GUID = Guid.NewGuid().ToString();
                            cbConfig.SysType = 1;
                            cbConfig.SysID = data.GUID;
                            sourcesystemBll.AddCallBack(cbConfig);//添加回调配置
                            string para = cbConfig.Paras;
                            string paraValues = cbConfig.ParaValues;
                            if (!string.IsNullOrEmpty(para))
                            {
                                List<string> paraArr = para.Split('&').ToList();
                                List<string> paraValuesArr = paraValues.Split('&').ToList();
                                for (int i = 0; i < paraArr.Count; i++)
                                {
                                    ETLCallBackPara paras = new ETLCallBackPara()
                                    {
                                        GUID = Guid.NewGuid().ToString(),
                                        KeyName = paraArr[i],
                                        KeyValue = paraValuesArr[i],
                                        ParentGUID = cbConfig.GUID
                                    };
                                    sourcesystemBll.AddCallBackParas(paras);//添加回调配置参数
                                }
                            }
                        }
                    }
                    #endregion
                    #region 联系人信息
                    string contacts = collection["Users"];
                    if (!string.IsNullOrEmpty(contacts))
                    {
                        string[] contactArr = contacts.Split(',');
                        foreach (string contactID in contactArr)
                        {
                            EtlContactInfo contactInfo = new EtlContactInfo()
                            {
                                GUID = Guid.NewGuid().ToString(),
                                UserID = contactID,
                                SysID = data.GUID,
                                SysType = 1
                            };
                            sourcesystemBll.AddContact(contactInfo);
                        }
                    }
                    #endregion

                }
                result = result + "," + data.GUID;
            }
            catch (Exception ex)
            {
                result = "操作失败." + ex.Message;
            }
            return result;
        }
        /// <summary>
        /// 删除源系统
        /// </summary>
        /// <param name="systemID"></param>
        /// <returns></returns>
        public ActionResult DeleteItem(string systemID, string dbName)
        {
            LoginInfo loginInfos = base.loginInfo();
            loginInfos.DBName = dbName;
            EtlSourceSystemBLL bll = new EtlSourceSystemBLL(loginInfos);
            if (bll.SourceDeleteCheck(systemID))
            {

                EtlSourceSystemInfo info = bll.GetInfoById(systemID);
                if (info.SourceType == 3)//access数据库删除本地文件
                {
                    string path = HttpContext.Server.MapPath("/files/" + info.SourceSysCode);
                    if (Directory.Exists(path))
                    {
                        string[] arr_file = Directory.GetFiles(path);
                        foreach (string s in arr_file)
                        {
                            System.IO.File.Delete(s);
                        }
                        Directory.Delete(path);
                    }
                }
                SqlParameter[] par = new SqlParameter[]
                {
                    new SqlParameter("@GUID",systemID) 
                };
                int i = bll.SourceDelete(par);//删除源系统信息

                new SysOperationLog(loginInfos).Log("源系统管理", Request.UserHostAddress, OperationAction.删除, OperationStatus.成功, "删除源系统信息操作,操作成功,ID=" + systemID, Request.Cookies["MDM_UserName"] != null ? Request.Cookies["MDM_UserName"].Value : "");
                return Json("OK");
            }
            else
                return Json("删除失败，存在依存关系！");

        }
        /// <summary>
        /// 取源系统连接字符串
        /// </summary>
        /// <param name="systemID"></param>
        /// <returns></returns>
        public JsonResult GetEtlSourceDbConfigList(string systemID, string dbName)
        {
            LoginInfo loginInfos = base.loginInfo();
            loginInfos.DBName = dbName;
            EtlSourceSystemBLL bll = new EtlSourceSystemBLL(loginInfos);
            SqlParameter[] para = new SqlParameter[]{
                new SqlParameter("@SysID", systemID),
                new SqlParameter("@Host", Request.Params["Host"]??""),
                new SqlParameter("@Server", Request.Params["Server"]??""),
                new SqlParameter("@Description", Request.Params["Description"]??"")
            };
            IList<ETLDbConfig> list = bll.GetETLDbConfigList(para);
            Dictionary<String, Object> jsonDict = new Dictionary<String, Object>();
            List<ETLDbInfo> dbinfo = new List<ETLDbInfo>();
            foreach (ETLDbConfig db in list)
            {
                ETLDbInfo info = new ETLDbInfo()
                {
                    GUID = db.GUID,
                    Host = db.DbDataSource,
                    Server = db.DbDatabase,
                    User = db.DbUserID,
                    Pass = encrypCode.Decrypto(db.DbPassword),
                    Part = db.DbPort.ToString(),
                    SID = db.DbSID,
                    FileUrl = db.DbDataSource,
                    FileUser = db.DbUserID,
                    FilePass = encrypCode.Decrypto(db.DbPassword),
                    WildCard = db.WildCard,
                    ExtractOrder = db.ExtractOrder,
                    Status = db.Status,
                    Description = db.Description ?? ""
                };
                dbinfo.Add(info);
            }
            jsonDict.Add("total", dbinfo.Count);
            jsonDict.Add("rows", dbinfo);
            return Json(jsonDict);
        }
        /// <summary>
        /// 取回调配置列表
        /// </summary>
        /// <param name="systemID"></param>
        /// <param name="dbName"></param>
        /// <returns></returns>
        public JsonResult GetCallBackConfigList(string systemID, string dbName)
        {
            LoginInfo loginInfos = base.loginInfo();
            loginInfos.DBName = dbName;
            EtlSourceSystemBLL bll = new EtlSourceSystemBLL(loginInfos);
            if (!string.IsNullOrEmpty(Request.Params["groupID"]))
                systemID = Request.Params["groupID"];
            IList<ETLCallBackConfig> list = bll.GetCallBackConfigList(new IDataParameter[] { new SqlParameter("@SysID", systemID) });
            foreach (ETLCallBackConfig model in list)
            {
                if (model.CallBackType == 2)
                {
                    IList<ETLCallBackPara> paras = bll.GetCallBackParasList(new IDataParameter[] { new SqlParameter("@ParentGUID", model.GUID) });
                    List<string> ps = new List<string>();
                    List<string> pvs = new List<string>();
                    foreach (ETLCallBackPara p in paras)
                    {
                        ps.Add(p.KeyName);
                        pvs.Add(p.KeyValue);
                    }
                    model.Paras = string.Join("&", ps);
                    model.ParaValues = string.Join("&", pvs);
                }
            }
            Dictionary<String, Object> jsonDict = new Dictionary<String, Object>();
            jsonDict.Add("total", list.Count);
            jsonDict.Add("rows", list);
            return Json(jsonDict);
        }
        /// <summary>
        /// 取通配符配置列表
        /// </summary>
        /// <param name="systemID"></param>
        /// <param name="dbName"></param>
        /// <returns></returns>
        public JsonResult GetWDCConfigList(string systemID, string dbName)
        {
            LoginInfo loginInfos = base.loginInfo();
            loginInfos.DBName = dbName;
            EtlSourceSystemBLL bll = new EtlSourceSystemBLL(loginInfos);
            IList<ETLWDCConfig> list = bll.GetWDCConfigList(new IDataParameter[] { new SqlParameter("@SysID", systemID) });
            Dictionary<String, Object> jsonDict = new Dictionary<String, Object>();

            jsonDict.Add("total", list.Count);
            jsonDict.Add("rows", list);
            return Json(jsonDict);
        }
        /// <summary>
        /// 取配置联系人集合
        /// </summary>
        /// <param name="systemID"></param>
        /// <param name="dbName"></param>
        /// <returns></returns>
        public JsonResult GetEtlSourceUserList(string systemID, string dbName)
        {
            LoginInfo loginInfos = base.loginInfo();
            loginInfos.DBName = dbName;
            EtlSourceSystemBLL bll = new EtlSourceSystemBLL(loginInfos);
            systemID = string.IsNullOrEmpty(systemID) ? "" : systemID;
            IList<SysUserInfo> list = bll.GetEtlSourceUserList(new IDataParameter[] { new SqlParameter("@SysID", systemID) });

            return Json(list);
        }

        public ActionResult SelectSourceList()
        {
            return View();
        }
        public ActionResult SendMsg(string systemID, string dbName)
        {
            try
            {

                SysUserBLL funBll = new SysUserBLL();
                string sysName = funBll.GetRTXSysName();
                if (string.IsNullOrEmpty(sysName))
                    return Json("消息接口已禁用，请开启RTX接口状态后重试。");
                string[] target = Request.Params["target"].Split(',');
                string title = Request.Params["title"];
                string type = Request.Params["type"];
                string SendMsgUrl = System.Configuration.ConfigurationManager.AppSettings["SendMsgUrl"];

                //消息路由信息。
                UrlRoute urlInfo = new UrlRoute();
                urlInfo.DBName = dbName;	//MDM_Platform_2_0
                urlInfo.EntityId = systemID;
                if (type == "1")
                    urlInfo.EntityName = "ETLSource"; //源系统或对接系统
                else
                    urlInfo.EntityName = "EtlTarget"; //源系统或对接系统
                urlInfo.Slug = "";
                urlInfo.Sign = "";
                urlInfo.Status = 1;

                UrlRouteBLL urlDAL = new UrlRouteBLL(base.loginInfo());

                MDP.SendMessage.SendRTXMessage send = new MDP.SendMessage.SendRTXMessage();
                StringBuilder msg = new StringBuilder();

                foreach (string tar in target)
                {
                    urlInfo.Id = Guid.NewGuid().ToString();
                    urlInfo.UserID = tar;	// 用户名
                    urlDAL.Insert(urlInfo);
                    string content = "";
                    if (type == "1")
                        content = string.Format("新增一条源系统信息需进行维护.\r\n源系统名称：{0}.\r\n地址：{1}ETLSource/?id={2}.\r\n消息来源：花样年地产主数据平台.", title, SendMsgUrl, urlInfo.Id);
                    else
                        content = string.Format("新增一条对接系统信息需进行维护.\r\n对接系统名称：{0}.\r\n地址：{1}EtlTarget/?id={2}.\r\n消息来源：花样年地产主数据平台.", title, SendMsgUrl, urlInfo.Id);
                    msg.Append(send.Send(sysName, tar, title, content, DateTime.Now.ToString(), ""));
                }
                return Json(string.Join("\r\n", msg));
            }
            catch (Exception ex)
            {
                return Json(ex.Message);
            }
        }
        public ActionResult UpDBStatus(string dbName)
        {
            try
            {
                string id = Request.Params["DBID"];
                int status = Request.Params["Status"] == "0" ? 1 : 0;
                LoginInfo loginInfos = base.loginInfo();
                loginInfos.DBName = dbName;
                EtlSourceSystemBLL bll = new EtlSourceSystemBLL(loginInfos);
                bll.UpDBStatus(id, status);
                return Json("1");
            }
            catch (Exception ex)
            {
                return Json(ex.Message);
            }
        }
        #endregion

        #region 源系统抽取配置

        #region EtlSourceViewConfig

        public ActionResult EtlSourceViewConfig(string systemID, string dbName)
        {
            //LoginInfo loginInfos = base.loginInfo();
            //loginInfos.DBName = dbName;
            //EtlSourceSystemBLL bll = new EtlSourceSystemBLL(loginInfos);
            //systemID = string.IsNullOrEmpty(systemID) ? "" : systemID;
            //var sourceSystemInfo = bll.GetEtlSourceSystemInfoById(systemID);
            PageRole role = new PageRole();
            role.role = SysPublicBLL.GetUserRole(Request.Cookies["MDM_UserName"].Value, dbName);
            return View(role);
        }
        /// <summary>
        /// 取配置的源系统表列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetSourceViewConfigList()
        {
            string dbName = Request.Params["dbName"];

            string order = Request.Params["order"];
            string sort = Request.Params["sort"];
            if (string.IsNullOrEmpty(sort))
                sort = "CreateTime";
            string rows = Request.Params["rows"];
            string page = Request.Params["page"];
            string systemID = Request.Params["systemID"];
            //列表数据
            LoginInfo loginInfos = base.loginInfo();
            loginInfos.DBName = dbName;
            int totalCount = 0;
            EtlSourceViewConfigBLL sourceViewConfigBLL = new EtlSourceViewConfigBLL(loginInfos);
            var list = sourceViewConfigBLL.GetViewConfigById(systemID, sort, order, Convert.ToInt32(page), Convert.ToInt32(rows), out totalCount);
            foreach (ETL_Source_ViewConfigInfo etlStep in list)
            {
                ETL_Task_List_History etlStepModel = sourceViewConfigBLL.GetStepStatus(etlStep.Guid);
                etlStep.StepStatus = etlStepModel.TaskStatus;
            }
            Dictionary<String, Object> jsonDict = new Dictionary<String, Object>();
            jsonDict.Add("total", totalCount);
            jsonDict.Add("rows", list);
            return Json(jsonDict);
        }
        /// <summary>
        /// 添加源系统表
        /// </summary> 
        /// <returns></returns>
        public int AddSourceViewConfig(string systemID, string dbName, FormCollection collection)
        {
            string dbType = Request.Params["DBType"];
            JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
            IList<ETL_Source_ViewConfigInfo> list = (IList<ETL_Source_ViewConfigInfo>)jsSerializer.Deserialize(collection["SourceViewConfig"], typeof(IList<ETL_Source_ViewConfigInfo>));
            //var list = this.GetSourceViewConfigs(collection);
            LoginInfo loginInfos = base.loginInfo();
            loginInfos.DBName = dbName;
            EtlSourceViewConfigBLL sourceViewConfigBll = new EtlSourceViewConfigBLL(loginInfos);
            systemID = string.IsNullOrEmpty(systemID) ? "" : systemID;

            EtlSourceSystemBLL bll = new EtlSourceSystemBLL(loginInfos);
            var sourceSystemInfo = bll.GetEtlSourceSystemInfoById(systemID);

            foreach (var sourceViewConfigInfo in list)
            {
                string displayName = sourceViewConfigInfo.ViewName.Replace(".", "_");
                sourceViewConfigInfo.SourceSystemID = systemID;
                sourceViewConfigInfo.DisplayName = displayName.Replace("[", "").Replace("]", "");
                sourceViewConfigInfo.MDP_ViewName = sourceViewConfigInfo.Guid.Replace("-", "");
                sourceViewConfigInfo.CreateUser = loginInfos.Dbuser;
                sourceViewConfigInfo.CreateTime = DateTime.Now;
                if (sourceSystemInfo.SourceType == 1 || sourceSystemInfo.SourceType == 2 || sourceSystemInfo.SourceType == 4 || sourceSystemInfo.SourceType == 8)
                    sourceViewConfigInfo.DBID = "";
                if (sourceSystemInfo != null && (sourceSystemInfo.SourceType.Equals(6) || sourceSystemInfo.SourceType.Equals(7)))
                {
                    sourceViewConfigInfo.FileFormat = "1";
                    sourceViewConfigInfo.TextQualifier = "<无>";
                    sourceViewConfigInfo.LineSeparator = "\\r\\n";
                    sourceViewConfigInfo.ColumnSeparator = ",";
                    sourceViewConfigInfo.SkipRowNum = 0;
                    sourceViewConfigInfo.SkipDataNum = 0;
                }
                sourceViewConfigBll.InsertSourceViewConfig(sourceViewConfigInfo);
                if (dbType == "2")//大数据添加[BigDateTagConfig]大数据标识配置
                    sourceViewConfigBll.AddBigDateTagConfig(sourceViewConfigInfo.Guid, sourceViewConfigInfo.SourceSystemID);
                new SysOperationLog(loginInfos).Log("源系统管理", Request.UserHostAddress, OperationAction.添加, OperationStatus.成功, "添加源系统表操作,操作成功,表名称=" + sourceViewConfigInfo.DisplayName, Request.Cookies["MDM_UserName"] != null ? Request.Cookies["MDM_UserName"].Value : "");
            }
            return 1;
        }
        /// <summary>
        /// 删除配置的源系统表
        /// </summary> 
        /// <returns></returns>
        public ActionResult DeleteSourceViewConfig(string systemID, string dbName, FormCollection collection)
        {
            JsonResponse<string> jsonResponse = new JsonResponse<string>();

            var list = this.GetSourceViewConfigs(collection);
            LoginInfo loginInfos = base.loginInfo();
            loginInfos.DBName = dbName;
            EtlSourceViewConfigBLL sourceViewConfigBll = new EtlSourceViewConfigBLL(loginInfos);
            systemID = string.IsNullOrEmpty(systemID) ? "" : systemID;
            var vcid = collection["vcid"];
            if (string.IsNullOrEmpty(vcid))
            {
                jsonResponse.Success = false;
                jsonResponse.Message = "传递参数错误(vcid)";
                jsonResponse.Data = "0";
            }
            else
            {
                var result = sourceViewConfigBll.DeleteSourceViewAndColumnConfig(vcid);
                jsonResponse.Success = true;
                jsonResponse.Message = "OK";
                jsonResponse.Data = result.ToString();
            }
            new SysOperationLog(loginInfos).Log("源系统管理", Request.UserHostAddress, OperationAction.删除, OperationStatus.成功, "删除源系统表操作,操作成功,ID=" + vcid, Request.Cookies["MDM_UserName"] != null ? Request.Cookies["MDM_UserName"].Value : "");
            return Json(jsonResponse);
        }
        public ActionResult GetETLStep(string viewID, string dbName)
        {

            LoginInfo loginInfos = base.loginInfo();
            loginInfos.DBName = dbName;
            EtlSourceViewConfigBLL sourceViewConfigBLL = new EtlSourceViewConfigBLL(loginInfos);
            ETL_Task_List_History etlStepModel = sourceViewConfigBLL.GetStepStatus(viewID);
            return Json(etlStepModel);
        }
        public ActionResult ViewETLData()
        {
            return View();
        }
        #endregion

        #region SourceTBList

        public ActionResult SourceTBList(string systemID, string dbName)
        {
            LoginInfo loginInfos = base.loginInfo();
            loginInfos.DBName = dbName;
            EtlSourceSystemBLL bll = new EtlSourceSystemBLL(loginInfos);
            IList<ETLWDCConfig> dblist = bll.GetWDCConfigList(new SqlParameter[] { new SqlParameter("@SysID", systemID) });
            List<string> list = new List<string>();
            if (dblist.Count > 0)
                list.Add("3");
            else
                list.Add("1");

            List<string> wildCard = new List<string>();
            foreach (ETLWDCConfig db in dblist)
            {
                wildCard.Add(db.WildCard);
            }
            list.Add(string.Join("$", wildCard.ToArray()));
            return View(list);
        }

        /// <summary>
        /// 获取源系统中的表列表
        /// </summary>
        /// <param name="id"></param>
        /// <param name="dbName"></param>
        /// <returns></returns>
        public ActionResult GetSourceTBList(string id, string dbName)
        {
            int totalCount = 0;
            List<ETL_Source_ViewConfigInfo> list = new List<ETL_Source_ViewConfigInfo>();
            Dictionary<String, Object> jsonDict = new Dictionary<String, Object>();
            try
            {
                string tableName = Request.Params["tableName"] ?? "";
                string wildCard = Request.Params["wildCard"] ?? "";
                string extNames = Request.Params["ExtNames"] ?? "";
                string order = Request.Params["order"];
                string sort = Request.Params["sort"];
                int rows = int.Parse(Request.Params["rows"].ToString());
                int page = int.Parse(Request.Params["page"].ToString());
                if (string.IsNullOrEmpty(sort))
                    sort = "";

                LoginInfo loginInfos = base.loginInfo();
                loginInfos.DBName = dbName;
                EtlSourceSystemBLL bll = new EtlSourceSystemBLL(loginInfos);
                var html = new System.Text.StringBuilder();

                DataTable dts = new DataTable();
                IList<ETLDbConfig> dbConfigList = bll.GetDbConfigBySysID(id);
                IList<ETLWDCConfig> wdcConfigList = bll.GetWDCConfigList(new SqlParameter[] { new SqlParameter("@SysID", id) });
                string connstr = string.Empty;
                string connType = string.Empty;
                #region 取数据库列表
                foreach (ETLDbConfig dbModel in dbConfigList)
                {
                    connstr = encrypCode.Decrypto(dbModel.DbConnectionStr);//源系统数据库连接字符串
                    connType = dbModel.DbType.ToString();

                    if (connType == "1" || connType == "2" || connType == "4" || connType == "8")
                    {
                        dts = bll.GetSourceViewTableBySql(connstr, connType);//取源系统数据表结果集
                        break;
                    }
                    else
                    {
                        if (dts.Columns.IndexOf("FileName") == -1)
                        {
                            dts.Columns.Add(new DataColumn("FileName", typeof(string)));
                            dts.Columns.Add(new DataColumn("FilePath", typeof(string)));
                            dts.Columns.Add(new DataColumn("DBID", typeof(string)));
                            dts.Columns.Add(new DataColumn("WildCardID", typeof(string)));
                        }
                        if (connType == "6" || connType == "7")
                        {
                            #region 平面文件
                            if (string.IsNullOrEmpty(wildCard))
                            {
                                foreach (ETLWDCConfig wdc_model in wdcConfigList)
                                {
                                    var row = dts.NewRow();
                                    row[0] = wdc_model.WildCard + "*";
                                    row[1] = dbModel.DbDataSource;
                                    row[2] = dbModel.GUID;
                                    row[3] = wdc_model.GUID;
                                    dts.Rows.Add(row);
                                }
                            }
                            else
                            {
                                var row = dts.NewRow();
                                row[0] = wildCard + "*";
                                row[1] = dbModel.DbDataSource;
                                row[2] = dbModel.GUID;
                                row[3] = wdcConfigList.First(o => { return o.WildCard == wildCard; }).GUID;
                                dts.Rows.Add(row);
                            }
                            #endregion
                        }
                        else if (connType == "3") //access
                        {
                            if (connstr.StartsWith("ftp"))
                            {
                                connstr = string.Format("/files/{0}/{1}", id.Replace("-", ""), dbModel.DbDataSource.Substring(dbModel.DbDataSource.LastIndexOf("/") + 1));//, dbpath.Substring(dbpath.LastIndexOf("/") + 1)
                                connstr = Server.MapPath(connstr);
                            }
                            else
                                connstr = dbModel.DbDataSource;
                            DataTable dt_n = bll.GetSourceViewTableBySql(connstr, connType);//取源系统数据表结果集
                            foreach (DataRow dr in dt_n.Rows)
                            {
                                var row = dts.NewRow();
                                row[0] = dr["Table_Name"];
                                row[1] = dr["description"]; ;
                                row[2] = dbModel.GUID;
                                row[3] = "";
                                dts.Rows.Add(row);
                            }
                        }
                        else if (connType == "5") // excel
                        {
                            #region  access or excel
                            string dbpath = dbModel.DbDataSource.ToLower();
                            string dbFileTg = "\\";
                            foreach (ETLWDCConfig wdc_model in wdcConfigList)
                            {
                                string wdc_m = wildCard.ToLower();
                                if (string.IsNullOrEmpty(wildCard))
                                {
                                    wdc_m = wdc_model.WildCard.ToLower();
                                }
                                else if (wildCard.ToLower() != wdc_model.WildCard.ToLower())
                                    break;
                                if (dbpath.StartsWith("ftp"))
                                {
                                    dbpath = string.Format("/files/{0}", id.Replace("-", ""));//, dbpath.Substring(dbpath.LastIndexOf("/") + 1)
                                    dbpath = Server.MapPath(dbpath);
                                    dbFileTg = "/";
                                }
                                string[] files = Directory.GetFiles(dbpath, wdc_m + "*");
                                foreach (string file in files)
                                {
                                    string fileName = file.Substring(file.LastIndexOf("\\") + 1).ToLower();
                                    if ((connType == "3" && (fileName.IndexOf(".mdb") > 0 || fileName.IndexOf("accdb") > 0)) ||
                                        (connType == "5" && (fileName.IndexOf(".xls") > 0 || fileName.IndexOf(".xlsx") > 0)))
                                    {
                                        DataTable dt_n = bll.GetSourceViewTableBySql(file, connType);//取源系统数据表结果集
                                        foreach (DataRow dr in dt_n.Rows)
                                        {
                                            var row = dts.NewRow();
                                            row[0] = string.Format("[{0}]", dr["Table_Name"]);
                                            row[1] = dbModel.DbDataSource + dbFileTg + fileName;
                                            row[2] = dbModel.GUID;
                                            row[3] = wdc_model.GUID;
                                            dts.Rows.Add(row);
                                        }
                                    }
                                }

                            }
                            #endregion
                        }
                    }
                }
                #endregion
                #region 页面渲染
                var sourceViewConfigList = bll.GetSourceViewConfigBySystemID(id);//已配置数据表结果集
                if (dts != null)
                {
                    foreach (DataRow row in dts.Rows)
                    {
                        var flg = false;
                        string tbDec = string.Empty;
                        string tbid = string.Empty;
                        string wcdid = string.Empty;
                        string viewName = row[0].ToString();
                        switch (connType)
                        {
                            case "1":
                                viewName = string.Format("[{0}].[{1}]", row[1], row[0]);
                                tbid = dbConfigList[0].GUID;
                                break;
                            case "2":
                            case "8":
                                viewName = string.Format("\"{0}\".\"{1}\"", row[1], row[0]);
                                tbid = dbConfigList[0].GUID;
                                break;
                            case "4":
                                viewName = string.Format("`{0}`.`{1}`", row[1], row[0]);
                                tbid = dbConfigList[0].GUID;
                                break;
                            default://非关系型数据库显示说明
                                tbDec = row[1].ToString();
                                tbid = row[2].ToString();
                                wcdid = row[3].ToString();
                                break;
                        }

                        List<string> extList = extNames.Split(',').ToList();
                        if (connType == "1" || connType == "2" || connType == "3" || connType == "4" || connType == "8")
                        {
                            if (extList.IndexOf(viewName) > -1 || sourceViewConfigList.FindIndex(o => { return o.ViewName == viewName; }) > -1)
                            {
                                flg = true;
                            }
                        }
                        else
                        {
                            if (sourceViewConfigList.FindIndex(o => { return o.ViewName == viewName && row[1].ToString().ToLower() == o.ViewChnName.ToLower(); }) > -1)
                            {
                                flg = true;
                            }
                        }
                        if (!flg)//渲染页面显示
                        {
                            list.Add(new ETL_Source_ViewConfigInfo()
                            {
                                Guid = Guid.NewGuid().ToString(),
                                ViewName = viewName,
                                ViewChnName = tbDec,
                                DBID = tbid,
                                WildCardID = wcdid
                            });
                        }

                    }
                }
                #endregion
                #region 分页 排序
                List<ETL_Source_ViewConfigInfo> olist = new List<ETL_Source_ViewConfigInfo>();
                tableName = tableName.ToLower();
                if (!string.IsNullOrEmpty(tableName))
                {
                    olist = (from c in list where c.ViewName.ToLower().Contains(tableName) select c).ToList<ETL_Source_ViewConfigInfo>();
                }
                if (order == "desc")
                {
                    if (sort == "ViewName")
                        olist = (from c in list where c.ViewName.ToLower().Contains(tableName) orderby c.ViewName descending select c).ToList<ETL_Source_ViewConfigInfo>();
                    else
                        olist = (from c in list where c.ViewName.ToLower().Contains(tableName) orderby c.ViewChnName descending select c).ToList<ETL_Source_ViewConfigInfo>();
                }
                else if (order == "asc")
                {
                    if (sort == "ViewName")
                        olist = (from c in list where c.ViewName.ToLower().Contains(tableName) orderby c.ViewName ascending select c).ToList<ETL_Source_ViewConfigInfo>();
                    else
                        olist = (from c in list where c.ViewName.ToLower().Contains(tableName) orderby c.ViewChnName ascending select c).ToList<ETL_Source_ViewConfigInfo>();
                }

                totalCount = olist.Count;
                if (page > -1 && rows > -1)
                {
                    olist = olist.Skip((page - 1) * rows).Take(rows).ToList();
                }
                #endregion
                jsonDict.Add("total", totalCount);
                jsonDict.Add("rows", olist);
            }
            catch (Exception ex)
            {
                //return "信息获取失败！" + ex.Message;
                jsonDict.Add("exMsg", ex.Message);
                return Json(jsonDict);
            }
            return Json(jsonDict);
        }
        #endregion

        #region SourceColumn
        /// <summary>
        /// 取所有字段类型
        /// </summary>
        /// <returns></returns>
        public ActionResult GetAllColumnTypeList()
        {
            StringBuilder wha = new StringBuilder();
            LoginInfo loginInfos = base.loginInfo();
            EtlSourceSystemBLL sourceSystemBLL = new EtlSourceSystemBLL(loginInfos);
            var allColumnTypeList = sourceSystemBLL.GetALLColumnTypeList();//取所有字段类型
            foreach (var columnTypeInfo in allColumnTypeList)
            {
                wha.Append(string.Format("<option value='{0}'>{1}</option>", columnTypeInfo.ColumnTypeName, columnTypeInfo.ColumnType));
            }
            return Json(wha.ToString());
        }
        public ActionResult SourceColumnEdit(string systemID, string viewID, string dbName, bool fileEdit = false)
        {
            LoginInfo loginInfos = base.loginInfo();
            loginInfos.DBName = dbName;
            EtlSourceSystemBLL bll = new EtlSourceSystemBLL(loginInfos);
            systemID = string.IsNullOrEmpty(systemID) ? "" : systemID;
            var sourceSystemInfo = bll.GetEtlSourceSystemInfoById(systemID);

            if ((sourceSystemInfo.SourceType.Equals(6) || sourceSystemInfo.SourceType.Equals(7)) && !fileEdit)
            {
                return RedirectToAction("SourceTBCsvFile", new { systemID = systemID, viewID = viewID, dbName = dbName });
            }

            EtlSourceViewConfigBLL sourceViewConfigBLL = new EtlSourceViewConfigBLL(loginInfos);

            var sourceViewConfigInfo = sourceViewConfigBLL.GetSourceViewConfigInfoByGuid(viewID);

            if (sourceSystemInfo.SourceType == 1 || sourceSystemInfo.SourceType == 2 || sourceSystemInfo.SourceType == 4 || sourceSystemInfo.SourceType == 8)
                sourceViewConfigInfo.IsFile = 0;
            else
                sourceViewConfigInfo.IsFile = 1;
            sourceViewConfigInfo.DisplayName = sourceViewConfigInfo.DisplayName.Replace("[", "").Replace("]", "");
            return View(sourceViewConfigInfo);
        }
        /// <summary>
        /// 配置源系统表字段信息
        /// </summary> 
        /// <returns></returns>
        public ActionResult GetSourceColumnList(string systemID, string viewID, string type, string dbName)
        {
            JsonResponse<string> jsonResponse = new JsonResponse<string>();
            try
            {
                string dbType = Request.Params["dbType"];
                LoginInfo loginInfos = base.loginInfo();
                loginInfos.DBName = dbName;
                EtlSourceSystemBLL sourceSystemBLL = new EtlSourceSystemBLL(loginInfos);
                EtlSourceViewConfigBLL sourceViewConfigBLL = new EtlSourceViewConfigBLL(loginInfos);
                var sourceViewConfigInfo = sourceViewConfigBLL.GetSourceViewConfigInfoByGuid(viewID);
                string viewName = sourceViewConfigInfo.ViewName;
                string viewid = sourceViewConfigInfo.Guid;
                string systemid = sourceViewConfigInfo.SourceSystemID;
                string tbOwer = "";

                var html = new System.Text.StringBuilder();

                #region 获取配置的数据库连接
                var dbConfigInfo = sourceSystemBLL.GetDbConfigByID(sourceViewConfigInfo.DBID);
                if (string.IsNullOrEmpty(sourceViewConfigInfo.DBID))
                    dbConfigInfo = sourceSystemBLL.GetDbConfigBySysID(systemid)[0];
                var connstr = "";
                var connType = "";
                bool isfileDB = true; //文件类型数据源
                connstr = encrypCode.Decrypto(dbConfigInfo.DbConnectionStr);
                connType = dbConfigInfo.DbType.ToString();

                if (connType == "5")//平面文件 Access Excel，解析数据库字符串
                {
                    if (connstr.ToLower().StartsWith("ftp"))
                    {
                        //例/files/1ac80ca3ea98462cb7f140055aac481e/etl_source_data.xls
                        connstr = "/files/" + systemID.Replace("-", "") + "/" + sourceViewConfigInfo.ViewChnName.Substring(sourceViewConfigInfo.ViewChnName.LastIndexOf("/") + 1);
                        connstr = Server.MapPath(connstr);
                    }
                    else
                        connstr = sourceViewConfigInfo.ViewChnName;//例:\\10.199.200.227\etl_source_data.xls
                }
                if (connType == "3")
                {
                    if (connstr.ToLower().StartsWith("ftp"))
                    {
                        connstr = "/files/" + systemID.Replace("-", "") + "/" + dbConfigInfo.DbDataSource.Substring(dbConfigInfo.DbDataSource.LastIndexOf("/") + 1);
                        connstr = Server.MapPath(connstr);
                    }
                    else
                        connstr = dbConfigInfo.DbDataSource;
                }

                #endregion
                #region 取已配置项和源系统项
                EtlSourceViewColumnConfigBLL etlSourceViewColumnConfigBLL = new EtlSourceViewColumnConfigBLL(loginInfos);
                //取已配置表字段列表 From IN ETL_Source_View_ColumnConfig表
                List<ETL_Source_View_ColumnConfigInfo> alreadyConfigList = etlSourceViewColumnConfigBLL.GetSourceViewColumnConfigByViewID(viewid);

                List<ETL_Source_View_ColumnConfigInfo> columnTypeList = new List<ETL_Source_View_ColumnConfigInfo>();
                //取源系统表字段列表
                List<ETL_Source_View_ColumnConfigInfo> allList = new List<ETL_Source_View_ColumnConfigInfo>();
                if (connType == "6" || connType == "7") //平面文件
                {
                    List<string> outfileList = new List<string>();
                    List<string> fileList = new List<string>();
                    string fileName = string.Empty;
                    string wdc = sourceViewConfigInfo.ViewName.Replace("*", "").ToLower();
                    if (dbConfigInfo.DbDataSource.ToLower().StartsWith("ftp"))
                    {
                        SysPublicBLL.readerFtp(dbConfigInfo.DbDataSource, dbConfigInfo.DbUserID, encrypCode.Decrypto(dbConfigInfo.DbPassword), out outfileList);
                        foreach (string s in outfileList)
                        {
                            if (s.ToLower().StartsWith(wdc))
                            {
                                fileName = s;
                                break;
                            }
                        }
                        fileName = dbConfigInfo.DbDataSource + "/" + fileName;
                    }
                    else
                    {
                        GetFileNames(dbConfigInfo.DbDataSource, connType == "6" ? ".txt" : ".csv", out outfileList);
                        foreach (string s in outfileList)
                        {
                            if (s.ToLower().StartsWith(wdc))
                            {
                                fileName = s;
                                break;
                            }
                        }
                        fileName = dbConfigInfo.DbDataSource + "\\" + fileName;
                    }
                    Stream stream = null;
                    WebResponse response = null;
                    if (connstr.ToLower().StartsWith("ftp"))
                    {
                        FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(fileName));
                        reqFTP.Credentials = new NetworkCredential(dbConfigInfo.DbUserID, encrypCode.Decrypto(dbConfigInfo.DbPassword));
                        reqFTP.KeepAlive = false;
                        reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
                        response = reqFTP.GetResponse();
                        stream = response.GetResponseStream();
                    }
                    else
                    {
                        stream = System.IO.File.Open(fileName, System.IO.FileMode.Open);
                    }
                    System.IO.StreamReader streamReader = new System.IO.StreamReader(stream, System.Text.Encoding.GetEncoding("gb2312"));

                    string[][] lineArr = read_txt(streamReader.ReadLine(), sourceViewConfigInfo.ColumnSeparator, sourceViewConfigInfo.TextQualifier);
                    List<string> headLineList = new List<string>();
                    foreach (string[] line in lineArr)
                    {
                        int ciso = 0;
                        foreach (string columnItem in line)
                        {
                            var sourceViewColumn = new ETL_Source_View_ColumnConfigInfo();
                            sourceViewColumn.ColumnName = columnItem;
                            sourceViewColumn.ColumnIsExtract = 1;
                            sourceViewColumn.ColumnType = "nvarchar(max)";
                            sourceViewColumn.ColumnIsPK = 0;
                            sourceViewColumn.ColumnOrderID = ciso;
                            allList.Add(sourceViewColumn);
                            ciso++;
                        }
                    }
                    columnTypeList = allList;
                }
                else
                {
                    if (tbOwer == "" && (connType == "2" || connType == "4" || connType == "8"))
                        tbOwer = viewName.Substring(0, viewName.IndexOf("."));
                    allList = sourceSystemBLL.GetTableInfoList(viewName, connstr, connType, tbOwer);
                    //取源系统表字段类型列表
                    columnTypeList = allList;
                    var columnPar = new SqlParameter[] { new SqlParameter("@viewName", viewName), };
                    if (connType != "3" && connType != "5")
                    {
                        isfileDB = false;
                        columnTypeList = etlSourceViewColumnConfigBLL.GetColumnInfoByOutSystem(columnPar, connstr, connType, tbOwer);
                        ETL_Source_View_ColumnConfigInfo addSourceType = new ETL_Source_View_ColumnConfigInfo();
                        addSourceType.ColumnOrderID = columnTypeList.Count + 1;
                        addSourceType.ColumnName = "MDP_SourceType";
                        addSourceType.ColumnType = "varchar(50)";
                        addSourceType.ColumnLength = 50;
                        addSourceType.ColumnPrecision = 0;
                        addSourceType.ColumnScale = 0;
                        addSourceType.ColumnIsNullable = 1;
                        addSourceType.ColumnIsPK = 0;
                        columnTypeList.Add(addSourceType);
                    }
                }
                #endregion
                #region 匹配字段与字段类型
                List<ETL_Source_View_ColumnConfigInfo> reloadList = new List<ETL_Source_View_ColumnConfigInfo>();
                if (alreadyConfigList.Count <= 0)
                {
                    foreach (ETL_Source_View_ColumnConfigInfo item in columnTypeList)
                    {
                        if (connType == "2" || connType == "4" || connType == "8")
                        {
                            if (item.ColumnType.IndexOf('(') > -1)
                            {
                                int endIndex = item.ColumnType.IndexOf('(');
                                char[] _arrayChar = item.ColumnType.ToCharArray();
                                item.ColumnType = "";
                                for (int p = 0; p < endIndex; p++)
                                {
                                    item.ColumnType += _arrayChar[p];
                                }
                            }
                            item.ColumnType = sourceViewConfigBLL.GetSqlTypeName(item.ColumnType, connType);//转换Oracle字段类型到sqlserver
                        }
                        ETL_Source_View_ColumnConfigInfo _temp = allList.Where(p => p.ColumnName.ToLower().Equals(item.ColumnName.ToLower())).FirstOrDefault();
                        if (_temp != null)
                        {
                            item.ColumnIsExtract = _temp.ColumnIsExtract;
                            item.ColumnIsOrder = _temp.ColumnIsOrder;
                            item.ColumnIsNullable = _temp.ColumnIsNullable;
                        }
                        alreadyConfigList.Add(item);
                    }
                }
                else if (type == "reLoad")//刷新，重加载
                {
                    int ciso = 0;
                    foreach (ETL_Source_View_ColumnConfigInfo item in columnTypeList)//匹配字段与字段类型
                    {
                        if (connType == "2" || connType == "4" || connType == "8")
                        {
                            if (item.ColumnType.IndexOf('(') > -1)
                            {
                                int endIndex = item.ColumnType.IndexOf('(');
                                char[] _arrayChar = item.ColumnType.ToCharArray();
                                item.ColumnType = "";
                                for (int p = 0; p < endIndex; p++)
                                {
                                    item.ColumnType += _arrayChar[p];
                                }
                            }
                            item.ColumnType = sourceViewConfigBLL.GetSqlTypeName(item.ColumnType, connType);
                        }
                        ETL_Source_View_ColumnConfigInfo _temp = alreadyConfigList.Where(p => p.ColumnName.ToLower().Equals(item.ColumnName.ToLower())).FirstOrDefault();
                        if (_temp == null)
                        {
                            item.ColumnIsExtract = 0;
                            item.ColumnOrderID = ciso;
                            reloadList.Add(item);
                        }
                        else
                        {
                            _temp.ColumnType = item.ColumnType;
                            _temp.ColumnLength = item.ColumnLength;
                            _temp.ColumnPrecision = item.ColumnPrecision;
                            _temp.ColumnScale = item.ColumnScale;
                            _temp.ColumnOrderID = ciso;

                            reloadList.Add(_temp);
                        }
                        ciso++;
                    }
                    alreadyConfigList = reloadList;
                }
                #endregion

                int j = 0;
                //不选中类型
                IList<string> notCheckType = new List<string>();
                notCheckType.Add("binary");
                notCheckType.Add("varbinary");
                notCheckType.Add("geography");
                notCheckType.Add("geometry");
                notCheckType.Add("hierarchyid");
                notCheckType.Add("image");
                //notCheckType.Add("timestamp");
                notCheckType.Add("text");
                notCheckType.Add("ntext");
                notCheckType.Add("xml");
                IList<string> notExtract = new List<string>();
                notExtract.Add("mdp_guid");
                notExtract.Add("mdp_batchtime");
                notExtract.Add("mdp_isdelete");
                notExtract.Add("mdp_operationtype");
                #region //渲染页面显示


                foreach (var model in alreadyConfigList)
                {
                    string typevalue = model.ColumnType;
                    typevalue = SysPublicBLL.FomatColumnType(model.ColumnType.ToLower(), model.ColumnLength.ToString(), model.ColumnPrecision, model.ColumnScale, connType);
                    string ntypevalue = typevalue;
                    string disable = string.Empty;
                    if (typevalue.IndexOf("(") > -1)
                        ntypevalue = typevalue.Substring(0, typevalue.IndexOf("("));

                    string _temp = notCheckType.Where(p => p.StartsWith(ntypevalue.ToLower())).FirstOrDefault();
                    string _temp2 = notExtract.Where(p => p.Equals(model.ColumnName.ToLower())).FirstOrDefault();
                    if (!string.IsNullOrEmpty(_temp) || !string.IsNullOrEmpty(_temp2))
                    {
                        disable = _temp2 = "disabled='disabled'";
                    }
                    string beChecked = string.Empty;
                    if (string.IsNullOrEmpty(disable) && model.ColumnIsExtract == 1)
                    {
                        beChecked = "checked=\"checked\"";
                    }
                    html.Append(string.Format("<tr><td><input name='Extract' class='Extract Extract{2}' type='checkbox' value='1' {0} {1}/><input type='hidden' name='Guid'value='{3}' /></td>", disable, beChecked, j, model.Guid));


                    //字段名
                    html.Append(string.Format("<td>{0}<input name='ColumnName' type='hidden' value=\"{0}\" /><input name='ColumnOrderID' type='hidden' value=\"{1}\" /></td>", model.ColumnName, model.ColumnOrderID));
                    //字段类型
                    html.Append(string.Format("<td><div style='position:relative;'><span style='margin-left:100px;width:22px;overflow:hidden;'>"));
                    html.Append(string.Format("<select class='columnTypeList Select{0}' style=\"width:118px;margin-left:-100px\" onchange=\"selShiftDesc('{0}');\" {1}></select></span>", j, _temp2));
                    html.Append(string.Format("<input class=\"type{0}\" name=\"ColumnType\" type='text' value=\"{1}\" style=\"width:95px;position:absolute;left:0px;\" {2}/></div></td>", j, typevalue, _temp2));

                    //主键
                    beChecked = "";
                    if (string.IsNullOrEmpty(disable) && model.ColumnIsPK == 1)
                    {
                        beChecked = "checked=\"checked\"";
                    }
                    html.Append(string.Format("<td align='center'><input class='PK PK{2}' name='PK' value='1' type='checkbox' {0} {1}/></td>", disable, beChecked, j));
                    //增量字段
                    if (!isfileDB)
                    {
                        beChecked = "";
                        if (string.IsNullOrEmpty(disable) && model.ColumnIsOrder == 1)
                        {
                            beChecked = "checked=\"checked\"";
                        }
                        html.Append(string.Format("<td align='center'><input title='{2}' class='Incremental{2}' value='1' name='Incremental' type='radio'  {0} {1}/></td>", disable, beChecked, j));
                    }
                    //大数据标识字段
                    if (!isfileDB && dbType == "2")
                    {
                        beChecked = "";
                        if (string.IsNullOrEmpty(disable) && model.ColumnIsBigDataTag == 1)
                        {
                            beChecked = "checked=\"checked\"";
                        }
                        html.Append(string.Format("<td align='center'><input title='{2}' class='ColumnIsBigDataTag{2}' value='1' name='ColumnIsBigDataTag' type='radio'  {0} {1}/></td>", disable, beChecked, j));
                    }

                    ////分页
                    //beChecked = "";
                    //if (string.IsNullOrEmpty(disable) && model.ColumnIsPaging == 1)
                    //{
                    //    beChecked = "checked=\"checked\"";
                    //}
                    //html.Append(string.Format("<td><input id='rowNum{2}' name='rowNum' type='radio' {0} {1}/></td>", disable, beChecked, j, model.ColumnName));
                    ////删除
                    //beChecked = "";
                    //if (string.IsNullOrEmpty(disable) && model.ColumnIsDelete == 1)
                    //{
                    //    beChecked = "checked=\"checked\"";
                    //}
                    //html.Append(string.Format("<td><input id='del{2}' name='del' type='radio' {0} {1}/></td>", disable, beChecked, j, model.ColumnName));
                    //说明
                    html.Append(string.Format("<td><input class='Name{1}' name='ColumnCNName' type='text'  value=\"{0}\"/></td></tr>", model.ColumnCNName, j));

                    j++;
                }
                #endregion
                jsonResponse.Data = html.ToString();
                jsonResponse.Success = true;
                return Json(jsonResponse);
            }
            catch (Exception ex)
            {
                jsonResponse.Message = "信息获取失败！" + ex.Message;
                jsonResponse.Success = false;
                return Json(jsonResponse);
            }
        }

        /// <summary>
        /// 保存源系统配置的列信息
        /// </summary>
        /// <param name="vcid"></param>
        /// <param name="displayName"></param>
        /// <param name="viewChnName"></param>
        /// <param name="tableMode"></param>
        /// <param name="Query"></param>
        /// <param name="Viewquery"></param>
        /// <param name="isauto"></param>
        /// <param name="dbName"></param>
        /// <returns></returns>
        public ActionResult SaveSoureView()
        {
            string viewID = Request.Params["viewID"];
            string dbName = Request.Params["dbName"];
            JsonResponse<string> jsonResponse = new JsonResponse<string>();
            LoginInfo loginInfos = base.loginInfo();
            loginInfos.DBName = dbName;
            SysPublicBLL pbll = new SysPublicBLL(loginInfos);
            DBDeploy dbInfo = pbll.GetDBInfo(dbName);
            if (Request.Cookies["MDM_UserName"] == null)
            {
                jsonResponse.Success = false;
                jsonResponse.Message = "请重新登录！";
                jsonResponse.Data = "0";
                return Json(jsonResponse);
            }
            else
            {
                try
                {
                    string displayName = Request.Params["displayName"];
                    string viewChnName = Request.Params["viewChnName"];
                    string BigDataTag = Request.Params["BigDataTag"];
                    string tableMode = Request.Params["tableMode"];
                    if (string.IsNullOrEmpty(tableMode))
                        tableMode = "1";
                    string initParas = Request.Params["init"];
                    string RoleType = Request.Params["RoleType"];
                    string IncrementNum = Request.Params["IncrementNum"];
                    string DateType = Request.Params["DateType"];

                    JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
                    IList<ETL_Source_View_ColumnEdit> QueryList = (IList<ETL_Source_View_ColumnEdit>)jsSerializer.Deserialize(Request.Params["Query"], typeof(IList<ETL_Source_View_ColumnEdit>));

                    #region 1、定义变量和初始化对象
                    EtlSourceSystemBLL sourceSystemBLL = new EtlSourceSystemBLL(loginInfos);
                    EtlSourceViewConfigBLL sourceViewConfigBLL = new EtlSourceViewConfigBLL(loginInfos);
                    EtlSourceViewColumnConfigBLL sourceViewColumnConfigBLL = new EtlSourceViewColumnConfigBLL(loginInfos);
                    var sourceViewConfigInfo = sourceViewConfigBLL.GetSourceViewConfigInfoByGuid(viewID);
                    EtlSourceSystemInfo sourceSystemInfo = sourceSystemBLL.GetEtlSourceSystemInfoById(sourceViewConfigInfo.SourceSystemID);

                    if (sourceViewConfigInfo == null)
                    {
                        // 配置的视图并不存在，不可以继续
                        jsonResponse.Success = false;
                        jsonResponse.Message = "配置的视图已经被删除，保存失败";
                        jsonResponse.Data = "0";
                        return Json(jsonResponse);
                    }
                    string SourceID = sourceViewConfigInfo.SourceSystemID;
                    if (sourceSystemInfo.SourceType == 3)
                        tableMode = "1";
                    #endregion

                    #region 2、解析页面值
                    //解析页面值   

                    string ct_msg = SysPublicBLL.CheckType(QueryList);//判断数据类型
                    if (ct_msg != "1")
                    {
                        jsonResponse.Success = false;
                        jsonResponse.Message = ct_msg.Replace("<br />", "\r\n");
                        jsonResponse.Data = "type";
                        return Json(jsonResponse);

                    }
                    #endregion

                    #region 参数检查
                    if (sourceViewConfigBLL.IsExistByDisplayNameWithOutGUID(viewID, displayName))
                    {
                        jsonResponse.Success = false;
                        jsonResponse.Message = string.Format("已经存在 {0} 相同的名称，请重新输入一个名称。", displayName);
                        jsonResponse.Data = "0";
                        return Json(jsonResponse);
                    }
                    if (!string.IsNullOrEmpty(BigDataTag) && sourceViewConfigBLL.IsExistByBigDataTag(viewID, BigDataTag))
                    {
                        jsonResponse.Success = false;
                        jsonResponse.Message = string.Format("已经存在相同的标识值，请重新输入一个标识。");
                        jsonResponse.Data = "0";
                        return Json(jsonResponse);
                    }
                    #endregion

                    #region 3、保存 ETL_Source_ViewConfig 表信息
                    sourceViewConfigInfo.DisplayName = displayName;
                    sourceViewConfigInfo.ViewChnName = viewChnName;
                    //sourceViewConfigInfo.BigDataTag = BigDataTag;
                    sourceViewConfigInfo.TableMode = int.Parse(tableMode);
                    sourceViewConfigInfo.IsDistinct = 0;
                    sourceViewConfigInfo.CreateTableScript = "";
                    sourceViewConfigInfo.UpdateUser = loginInfos.Dbuser;
                    sourceViewConfigInfo.UpdateTime = DateTime.Now;

                    sourceViewConfigInfo.MDP_ViewName = sourceViewConfigInfo.Guid.Replace("-", "");

                    sourceViewConfigBLL.UpdateSourceViewConfig(sourceViewConfigInfo);
                    IncrementRole irole = new IncrementRole();
                    irole.ViewID = viewID;
                    irole.RoleType = RoleType;
                    irole.DateType = DateType;
                    irole.IncrementNum = IncrementNum;
                    sourceViewConfigBLL.UpdateIncrementRole(irole);
                    #endregion

                    #region 4、保存 ETL_Source_View_ColumnConfig 表信息

                    //先删除所有配置信息
                    //sourceViewColumnConfigBLL.DeleteSourceViewColumnConfigByViewID(viewID);
                    EtlSourceViewColumnConfigBLL etlSourceViewColumnConfigBLL = new EtlSourceViewColumnConfigBLL(loginInfos);
                    //取已配置表字段列表 From IN ETL_Source_View_ColumnConfig表
                    List<ETL_Source_View_ColumnConfigInfo> savedColumnList = etlSourceViewColumnConfigBLL.GetSourceViewColumnConfigByViewID(viewID);
                    foreach (ETL_Source_View_ColumnConfigInfo needDelitem in savedColumnList)
                    {
                        var delItem = QueryList.FirstOrDefault(o => { return o.Guid == needDelitem.Guid; });
                        if (delItem == null)//删除元素
                        {
                            sourceViewColumnConfigBLL.DeleteSourceViewColumnConfigByViewID(needDelitem.Guid);
                        }
                    }
                    foreach (ETL_Source_View_ColumnEdit columnitem in QueryList)
                    {
                        string columnType = columnitem.ColumnType;
                        int pk = columnitem.PK == "1" ? 1 : 0;
                        int Incremental = columnitem.Incremental == "1" ? 1 : 0;
                        int columnLength = 0;
                        int columnPrecision = 0;
                        int columnScale = 0;
                        int Extract = columnitem.Extract == "1" ? 1 : 0;

                        #region 解析字段类型
                        string typevalue = "";
                        string[] typeArr = columnType.Replace(")", "").Split('(');
                        typevalue = typeArr[0].ToLower();
                        if (typeArr.Length > 1)
                        {
                            string[] lengthArr = typeArr[1].Split(',');
                            if (lengthArr.Length == 1)
                            {
                                if (lengthArr[0].ToUpper().Equals("MAX"))
                                {
                                    columnLength = -1;
                                }
                                else
                                {
                                    int.TryParse(lengthArr[0], out columnLength);
                                }
                            }
                            else
                            {
                                if (typevalue.ToLower().Equals("numeric") || typevalue.ToLower().Equals("decimal"))
                                {
                                    int.TryParse(lengthArr[0], out columnLength);
                                    int.TryParse(lengthArr[0], out columnPrecision);
                                    int.TryParse(lengthArr[1], out columnScale);
                                }
                            }
                        }
                        #endregion
                        if (Extract == 1 && !string.IsNullOrEmpty(typevalue.Trim()))//保存字段信息
                        {
                            bool isInsert = true;
                            string cid = Guid.NewGuid().ToString();
                            if (columnitem.Guid.Length == 36)
                            {
                                isInsert = false;
                                cid = columnitem.Guid;
                            }
                            ETL_Source_View_ColumnConfigInfo sourceViewColumnInfo = new ETL_Source_View_ColumnConfigInfo()
                            {
                                Guid = cid,
                                ViewID = viewID,
                                ColumnName = columnitem.ColumnName,
                                ColumnType = typevalue.Trim(),
                                ColumnIsPK = pk,
                                ColumnIsOrder = Incremental,
                                ColumnOrderID = columnitem.ColumnOrderID,
                                ColumnLength = columnLength,
                                ColumnPrecision = columnPrecision,
                                ColumnIsNullable = pk == 1 ? 0 : 1,
                                CreateUser = "",
                                CreateTime = DateTime.Now,
                                ColumnScale = columnScale,
                                ColumnCNName = columnitem.ColumnCNName,
                                ColumnIsExtract = Extract,
                                ColumnIsBigDataTag = columnitem.ColumnIsBigDataTag == "1" ? 1 : 0
                            };
                            if (isInsert)//添加
                                sourceViewColumnConfigBLL.InsertSourceViewColumnConfig(sourceViewColumnInfo);
                            else//更新
                                sourceViewColumnConfigBLL.UpdateSourceViewColumnConfig(sourceViewColumnInfo);
                        }
                    }

                    #endregion

                    #region 5.大数据量保存字段初始化值
                    if (dbInfo.DBType == 2)
                    {
                        sourceViewConfigBLL.DeleteInitBigData(viewID);
                        List<string> paraArr = initParas.Split('&').ToList();
                        for (int pai = 0; pai < paraArr.Count; pai++)
                        {
                            SqlParameter[] para = new SqlParameter[] { 
                        new SqlParameter("@ViewID",viewID)
                        ,new SqlParameter("@ColumnValue",paraArr[pai])
                        ,new SqlParameter("@SortNum",pai+1) 
                        };
                            sourceViewConfigBLL.AddInitBigData(para);
                        }
                    }
                    #endregion
                    int result = sourceViewConfigBLL.CreateStgTB(viewID);//创建 CreateStgTB

                    #region 5、创建 CreateStgTB 失败，回滚任务
                    if (result == -1)
                    {
                        sourceViewConfigBLL.RollBackSourceViewConfig(viewID, false);
                        jsonResponse.Success = false;
                        jsonResponse.Message = "保存失败，请重新设置";
                        jsonResponse.Data = result.ToString();
                        return Json(jsonResponse);
                    }

                    #endregion

                    #region 6、添加任务步骤

                    var sourceSystemBLLbll = new EtlSourceSystemBLL(loginInfos);
                    result = sourceSystemBLLbll.InsertJobConfigStep(viewID, "");
                    if (result == 1)
                    {
                        new SysOperationLog(loginInfos).Log("源系统管理", Request.UserHostAddress, OperationAction.更新, OperationStatus.成功, "更新源系统配置信息操作,操作成功,ViewID=" + viewID,
                            Request.Cookies["MDM_UserName"] != null ? Request.Cookies["MDM_UserName"].Value : "");
                    }
                    #endregion

                    jsonResponse.Success = true;
                    jsonResponse.Message = "保存成功";
                    jsonResponse.Data = result.ToString();
                    return Json(jsonResponse);
                }
                catch (Exception ex)
                {
                    new SysOperationLog(loginInfos).Log("源系统管理", Request.UserHostAddress, OperationAction.更新, OperationStatus.失败, "更新源系统表配置信息操作,操作失败,ViewID=" + viewID + "," + ex.Message,
                        Request.Cookies["MDM_UserName"] != null ? Request.Cookies["MDM_UserName"].Value : "");
                    jsonResponse.Success = false;
                    jsonResponse.Message = ex.Message;
                    jsonResponse.Data = "0";
                    return Json(jsonResponse);
                }
            }
        }

        public ActionResult GetAllInit(string viewID, string dbName)
        {
            LoginInfo loginInfos = base.loginInfo();
            loginInfos.DBName = dbName;
            EtlSourceViewConfigBLL sourceViewConfigBLL = new EtlSourceViewConfigBLL(loginInfos);

            List<BigDataSouceViewInit> initList = sourceViewConfigBLL.GetAllInit(viewID);
            return Json(initList);
        }
        public ActionResult GetIncrementRole(string viewID, string dbName)
        {
            LoginInfo loginInfos = base.loginInfo();
            loginInfos.DBName = dbName;
            EtlSourceViewConfigBLL sourceViewConfigBLL = new EtlSourceViewConfigBLL(loginInfos);

            IncrementRole item = sourceViewConfigBLL.GetIncrementRole(viewID);
            return Json(item);
        }
        #endregion

        #region SourceTBData
        public ActionResult SourceTBData(string systemID, string dbName)
        {
            LoginInfo loginInfos = base.loginInfo();
            loginInfos.DBName = dbName;
            EtlSourceSystemBLL bll = new EtlSourceSystemBLL(loginInfos);
            systemID = string.IsNullOrEmpty(systemID) ? "" : systemID;
            return View();
        }
        public string GetSourceTableHeadData(DataColumnCollection columnList)
        {
            StringBuilder sb = new StringBuilder();
            StringBuilder sbOpt = new StringBuilder();
            LoginInfo loginInfos = base.loginInfo();
            IList<Operator> optList = OperatorBLL.GetList();
            sbOpt.Append("<option value=''>-请选择-</option>");
            foreach (Operator o in optList)
            {
                sbOpt.Append(string.Format("<option value='{0}'>{0}</option>", o.LegalOperator));
            }
            sb.Append("<table class='pagTable' cellpadding='0' cellspacing='0'>");
            foreach (DataColumn dc in columnList)
            {
                sb.Append(string.Format(@"<tr id='{0}'><td>{0}</td><td width='100px' align='center'><select class='sel_{0}'>{1}</select></td>
<td width='160px'><input width='150px' type='text' class='txt_{0}'><input type='hidden' class='ctp_{0}' value='{2}'></td></tr>",
dc.ColumnName, sbOpt.ToString(), dc.DataType.Name));
            }
            sb.Append("</table>");
            return sb.ToString();
        }
        #endregion

        #region SourceLocalTBData
        public ActionResult SourceLocalTBData(string systemID, string viewID, string dbName)
        {
            return View();
        }

        public ActionResult GetLocalTableData(string systemID, string viewID, string dbName)
        {
            JsonResponse<string> jsonResponse = new JsonResponse<string>();
            int totalCount = 0;
            Dictionary<String, Object> jsonDict = new Dictionary<String, Object>();
            try
            {
                #region 声明变量与参数

                string order = Request.Params["order"];
                if (order == null)
                    order = string.Empty;
                string sort = Request.Params["sort"];
                if (sort == null)
                    sort = "1";

                string query = Request.Params["query"];
                List<string> sql = new List<string>();
                List<SqlParameter> sqlPara = new List<SqlParameter>();
                if (!string.IsNullOrEmpty(query) && query != "[]")
                {
                    query = query.Replace("\"", "").Replace("[[", "").Replace("]]", "");
                    IEnumerable<string> arrList = query.Split("],[");

                    int i = 0;
                    foreach (string str in arrList)
                    {
                        string[] item = str.Split(',');
                        switch (item[1])
                        {
                            case "like":
                            case "not like":
                                sql.Add(string.Format(" {0} {1} '%'+@{0}+'%' ", item[0], item[1]));
                                break;
                            case "is null":
                            case "is NOT null":
                                sql.Add(string.Format(" {0} {1} ", item[0], item[1]));
                                break;
                            case "in":
                            case "not in":
                                sql.Add(string.Format(" {0} {1} (@{0}) ", item[0], item[1]));
                                break;
                            default:
                                sql.Add(string.Format(" {0} {1} @{0} ", item[0], item[1]));// " column=@column"
                                break;
                        }
                        sqlPara.Add(new SqlParameter("@" + item[0], item[2]));
                        i++;
                    }
                }

                string strTableName = string.Empty;
                LoginInfo loginInfos = base.loginInfo();
                loginInfos.DBName = dbName;
                SysPublicBLL publicBll = new SysPublicBLL(loginInfos);
                DBDeploy dbInfo = publicBll.GetDBInfo(dbName);
                EtlSourceSystemBLL sourceSystemBLL = new EtlSourceSystemBLL(loginInfos);
                EtlSourceViewConfigBLL sourceViewConfigBLL = new EtlSourceViewConfigBLL(loginInfos);
                EtlSourceViewColumnConfigBLL sourceViewColumnConfigBLL = new EtlSourceViewColumnConfigBLL(loginInfos);

                #endregion

                #region 读取列信息
                var sourceViewConfigInfo = sourceViewConfigBLL.GetSourceViewConfigInfoByGuid(viewID);
                if (sourceViewConfigInfo == null || dbInfo.DBType == 2)
                {
                    jsonResponse.Success = false;
                    jsonResponse.Message = "查询结果无数据！";
                    jsonResponse.Data = "";
                    return Json(jsonResponse);
                }
                strTableName = string.Format("[{0}].[{1}]", sourceViewConfigInfo.MDP_ViewSchema, sourceViewConfigInfo.MDP_ViewName);
                var columnList = sourceViewColumnConfigBLL.GetSourceViewColumnConfigByViewID(viewID);

                if (columnList == null || columnList.Count == 0)
                {
                    jsonResponse.Success = false;
                    jsonResponse.Message = "查询结果无数据！";
                    jsonResponse.Data = "";
                    return Json(jsonResponse);
                }
                #endregion

                #region 拼接列数据
                var columnNameDics = columnList.ToDictionary(p => p.ColumnName.ToUpper(), p => p.ColumnCNName);

                string strColumn = string.Empty;
                foreach (var columnInfo in columnList)
                {
                    strColumn += string.Format("[{0}],", columnInfo.ColumnName);
                }
                if (strColumn.EndsWith(","))
                {
                    strColumn = strColumn.Remove(strColumn.Length - 1, 1);
                }
                if (!string.IsNullOrEmpty(order) && !strColumn.Contains(order))
                {
                    jsonResponse.Success = false;
                    jsonResponse.Message = "排序的列并不存在于该表中，请刷新后重试";
                    jsonResponse.Data = "";
                    return Json(jsonResponse);
                }
                #endregion

                #region 读取本地数据表数据
                var dt = sourceSystemBLL.GetLocalTableData(strTableName, strColumn, order, (sort.Equals("1") ? true : false), 15, sql.ToArray(), sqlPara);
                var html = new System.Text.StringBuilder();

                var strDT = this.DataTableToString(dt, columnNameDics, order, sort, "initLocalTableData");
                jsonResponse.Head = GetSourceTableHeadData(dt.Columns);
                jsonResponse.Success = true;
                jsonResponse.Message = "OK";
                jsonResponse.Data = strDT;

                #endregion
                return Json(jsonResponse);
            }
            catch (Exception ex)
            {
                //return "信息获取失败！" + ex.Message;
                jsonResponse.Success = false;
                jsonResponse.Message = "信息获取失败！" + ex.Message;
                jsonResponse.Data = "";
                return Json(jsonResponse);
            }
        }

        public ActionResult GetCmpCloumnData(string systemID, string viewID, string dbName)
        {
            JsonResponse<string> jsonResponse = new JsonResponse<string>();

            Dictionary<String, Object> jsonDict = new Dictionary<String, Object>();
            try
            {

                jsonResponse.Success = true;
                jsonResponse.Message = "";
                jsonResponse.Data = GetSourceTBColumnInfo(viewID, dbName);
                return Json(jsonResponse);
            }
            catch (Exception ex)
            {
                jsonResponse.Success = false;
                jsonResponse.Message = "信息获取失败！" + ex.Message;
                jsonResponse.Data = "";
                return Json(jsonResponse);
            }
        }

        public string GetSourceTBColumnInfo(string viewID, string dbName)
        {
            LoginInfo loginInfos = base.loginInfo();
            loginInfos.DBName = dbName;
            EtlSourceSystemBLL sourceSystemBLL = new EtlSourceSystemBLL(loginInfos);
            EtlSourceViewConfigBLL sourceViewConfigBLL = new EtlSourceViewConfigBLL(loginInfos);
            var sourceViewConfigInfo = sourceViewConfigBLL.GetSourceViewConfigInfoByGuid(viewID);
            string viewName = sourceViewConfigInfo.ViewName;
            string systemID = sourceViewConfigInfo.SourceSystemID;
            string tbOwer = "";

            #region 获取配置的数据库连接
            var dbConfigInfo = sourceSystemBLL.GetDbConfigByID(sourceViewConfigInfo.DBID);
            if (string.IsNullOrEmpty(sourceViewConfigInfo.DBID))
                dbConfigInfo = sourceSystemBLL.GetDbConfigBySysID(systemID)[0];
            var connstr = "";
            var connType = "";
            connstr = encrypCode.Decrypto(dbConfigInfo.DbConnectionStr);
            connType = dbConfigInfo.DbType.ToString();

            if (connType == "5")//平面文件 Access Excel，解析数据库字符串
            {
                if (connstr.ToLower().StartsWith("ftp"))
                {//例/files/1ac80ca3ea98462cb7f140055aac481e/etl_source_data.xls
                    connstr = "/files/" + systemID.Replace("-", "") + "/" + sourceViewConfigInfo.ViewChnName.Substring(sourceViewConfigInfo.ViewChnName.LastIndexOf("/") + 1);
                    connstr = Server.MapPath(connstr);
                }
                else
                    connstr = sourceViewConfigInfo.ViewChnName;//例:\\10.199.200.227\etl_source_data.xls
            }
            if (connType == "3")
            {
                if (connstr.ToLower().StartsWith("ftp"))
                {
                    connstr = "/files/" + systemID.Replace("-", "") + "/" + dbConfigInfo.DbDataSource.Substring(dbConfigInfo.DbDataSource.LastIndexOf("/") + 1);
                    connstr = Server.MapPath(connstr);
                }
                else
                    connstr = dbConfigInfo.DbDataSource;
            }

            #endregion
            #region 取已配置项和源系统项
            EtlSourceViewColumnConfigBLL etlSourceViewColumnConfigBLL = new EtlSourceViewColumnConfigBLL(loginInfos);
            //取已配置表字段列表 From IN ETL_Source_View_ColumnConfig表
            List<ETL_Source_View_ColumnConfigInfo> alreadyConfigList = etlSourceViewColumnConfigBLL.GetSourceViewColumnConfigByViewID(viewID);
            List<ETL_Source_View_ColumnConfigInfo> columnTypeList = new List<ETL_Source_View_ColumnConfigInfo>();
            //取源系统表字段列表
            List<ETL_Source_View_ColumnConfigInfo> allList = new List<ETL_Source_View_ColumnConfigInfo>();
            if (connType == "6" || connType == "7") //平面文件
            {
                List<string> outfileList = new List<string>();
                List<string> fileList = new List<string>();
                string fileName = string.Empty;
                if (dbConfigInfo.DbDataSource.StartsWith("ftp"))
                    SysPublicBLL.readerFtp(dbConfigInfo.DbDataSource, dbConfigInfo.DbUserID, encrypCode.Decrypto(dbConfigInfo.DbPassword), out outfileList);
                else
                {
                    GetFileNames(dbConfigInfo.DbDataSource, connType == "6" ? ".txt" : ".csv", out outfileList);
                }
                string wdc = sourceViewConfigInfo.ViewName.Replace("*", "").ToLower();
                foreach (string s in outfileList)
                {
                    if (s.ToLower().StartsWith(wdc))
                    {
                        fileName = s;
                        break;
                    }
                }
                if (dbConfigInfo.DbDataSource.ToLower().StartsWith("ftp"))
                    fileName = dbConfigInfo.DbDataSource + "/" + fileName;
                else
                    fileName = dbConfigInfo.DbDataSource + "\\" + fileName;
                Stream stream = null;
                WebResponse response = null;
                if (connstr.ToLower().StartsWith("ftp"))
                {
                    FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(fileName));
                    reqFTP.Credentials = new NetworkCredential(dbConfigInfo.DbUserID, encrypCode.Decrypto(dbConfigInfo.DbPassword));
                    reqFTP.KeepAlive = false;
                    reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
                    response = reqFTP.GetResponse();
                    stream = response.GetResponseStream();
                }
                else
                {
                    stream = System.IO.File.Open(fileName, System.IO.FileMode.Open);
                }
                System.IO.StreamReader streamReader = new System.IO.StreamReader(stream, System.Text.Encoding.GetEncoding("gb2312"));
                string[][] lineArr = read_txt(streamReader.ReadLine(), sourceViewConfigInfo.ColumnSeparator, sourceViewConfigInfo.TextQualifier);
                foreach (string[] line in lineArr)
                {
                    foreach (string columnItem in line)
                    {
                        var sourceViewColumn = new ETL_Source_View_ColumnConfigInfo();
                        sourceViewColumn.ColumnName = columnItem;
                        sourceViewColumn.ColumnIsExtract = 1;
                        sourceViewColumn.ColumnType = "nvarchar(max)";
                        sourceViewColumn.ColumnIsPK = 0;
                        sourceViewColumn.ColumnIsOrder = 0;
                        allList.Add(sourceViewColumn);
                    }
                }
                columnTypeList = allList;
            }
            else
            {
                if (tbOwer == "" && (connType == "2" || connType == "4" || connType == "8"))
                    tbOwer = viewName.Substring(0, viewName.IndexOf("."));
                allList = sourceSystemBLL.GetTableInfoList(viewName, connstr, connType, tbOwer);
                //取源系统表字段类型列表
                columnTypeList = allList;
                var columnPar = new SqlParameter[] { new SqlParameter("@viewName", viewName), };
                if (connType != "3" && connType != "5")
                    columnTypeList = etlSourceViewColumnConfigBLL.GetColumnInfoByOutSystem(columnPar, connstr, connType, tbOwer);

            }
            #endregion

            StringBuilder sb = new StringBuilder();
            foreach (ETL_Source_View_ColumnConfigInfo item in columnTypeList)//匹配字段与字段类型
            {
                if (connType == "2" || connType == "4" || connType == "8")
                {
                    if (item.ColumnType.IndexOf('(') > -1)
                    {
                        int endIndex = item.ColumnType.IndexOf('(');
                        char[] _arrayChar = item.ColumnType.ToCharArray();
                        item.ColumnType = "";
                        for (int p = 0; p < endIndex; p++)
                        {
                            item.ColumnType += _arrayChar[p];
                        }
                    }
                    //item.ColumnType = sourceViewConfigBLL.GetSqlTypeName(item.ColumnType, connType);
                }
                item.ColumnType = SysPublicBLL.FomatColumnType(item.ColumnType.ToLower(), item.ColumnLength.ToString(), item.ColumnPrecision, item.ColumnScale, connType);
                ETL_Source_View_ColumnConfigInfo _temp = alreadyConfigList.Where(p => p.ColumnName.ToLower().Equals(item.ColumnName.ToLower())).FirstOrDefault();
                string pk = item.ColumnIsPK == 1 ? "是" : "否";
                string _pk = string.Empty;
                string _ty = string.Empty;
                if (_temp == null)
                {
                    item.ColumnType = string.Format("<font color='#ff0000'>{0}</font>", item.ColumnType);
                    pk = string.Format("<font color='#ff0000'>{0}</font>", pk);
                }
                else
                {
                    _pk = _temp.ColumnIsPK == 1 ? "是" : "否";
                    _ty = SysPublicBLL.FomatColumnType(_temp.ColumnType.ToLower(), _temp.ColumnLength.ToString(), _temp.ColumnPrecision, _temp.ColumnScale, connType);
                    if (_temp.ColumnIsPK != item.ColumnIsPK)
                        pk = string.Format("<font color='#ff0000'>{0}</font>", pk);
                    if (_ty != item.ColumnType)
                        item.ColumnType = string.Format("<font color='#ff0000'>{0}</font>", item.ColumnType);
                }
                sb.Append(string.Format("<tr><td>{0}</td><td>{1}</td><td align='center'>{2}</td><td>{3}</td><td align='center'>{4}</td></tr>",
                    item.ColumnName, item.ColumnType, pk, _ty, _pk));
            }
            return sb.ToString();
        }
        #endregion

        #region SourceTBCsvFile

        public ActionResult SourceTBCsvFile(string systemID, string viewID, string dbName)
        {
            LoginInfo loginInfos = base.loginInfo();
            loginInfos.DBName = dbName;
            EtlSourceSystemBLL bll = new EtlSourceSystemBLL(loginInfos);
            systemID = string.IsNullOrEmpty(systemID) ? "" : systemID;
            var sourceSystemInfo = bll.GetEtlSourceSystemInfoById(systemID);
            EtlSourceViewConfigBLL sourceViewConfigBLL = new EtlSourceViewConfigBLL(loginInfos);
            ETL_Source_ViewConfigInfo sourceViewConfigInfo = sourceViewConfigBLL.GetSourceViewConfigInfoByGuid(viewID);
            ETLDbConfig dbConfig = bll.GetDbConfigByID(sourceViewConfigInfo.DBID);
            List<string> outfileList = new List<string>();
            List<string> fileList = new List<string>();

            if (sourceSystemInfo.SourceType == 6 || sourceSystemInfo.SourceType == 7)
            {
                if (dbConfig.DbDataSource.StartsWith("ftp"))
                    SysPublicBLL.readerFtp(dbConfig.DbDataSource, dbConfig.DbUserID, encrypCode.Decrypto(dbConfig.DbPassword), out outfileList);
                else
                {
                    GetFileNames(dbConfig.DbDataSource, sourceSystemInfo.SourceType == 6 ? ".txt" : ".csv", out outfileList);
                }
                string wdc = sourceViewConfigInfo.ViewName.Replace("*", "").ToLower();
                foreach (string s in outfileList)
                {
                    if (s.ToLower().StartsWith(wdc))
                        fileList.Add(s);
                }
                sourceViewConfigInfo.ViewChnName = string.Join("$", fileList);
                sourceViewConfigInfo.IsFile = sourceSystemInfo.SourceType;
            }
            return View(sourceViewConfigInfo);
        }
        public ActionResult PreviewUpLoadData(FormCollection collection)
        {
            JsonResponse<string> jsonResponse = new JsonResponse<string>();

            #region 获取 Post 参数
            string dbName = base.Request.Form["dbName"].Trim();
            string systemID = base.Request.Form["systemID"].Trim();
            string viewID = base.Request.Form["viewID"].Trim();
            string viewName = base.Request.Form["viewName"].Trim();
            // 行分隔符
            string lineSeparator = base.Request.Form["lineSeparator"].Trim();
            // 列分隔符
            string columnSeparator = base.Request.Form["columnSeparator"].Trim();
            // 文本限定符
            string textQualifier = base.Request.Form["textQualifier"].Trim();
            // 要跳过的标题行数
            int skipRowNum = Convert.ToInt32(base.Request.Form["skipRowNum"].Trim());
            // 要跳过的数据行数
            int skipDataNum = Convert.ToInt32(base.Request.Form["skipDataNum"].Trim());
            #endregion
            try
            {
                LoginInfo loginInfos = base.loginInfo();
                loginInfos.DBName = dbName;
                EtlSourceSystemBLL sourceSystemBLL = new EtlSourceSystemBLL(loginInfos);

                EtlSourceViewConfigBLL sourceViewConfigBLL = new EtlSourceViewConfigBLL(loginInfos);
                var sourceViewConfigInfo = sourceViewConfigBLL.GetSourceViewConfigInfoByGuid(viewID);

                // 获取数据库串
                var dbConfig = sourceSystemBLL.GetDbConfigByID(sourceViewConfigInfo.DBID);
                if (dbConfig.DbType == 7)
                {
                    textQualifier = "\"";
                    lineSeparator = "\\r\\n";
                    columnSeparator = ",";
                }
                jsonResponse.Success = true;
                jsonResponse.Data = ReadTxtData(dbConfig.DbDataSource, viewName, dbConfig.DbUserID, encrypCode.Decrypto(dbConfig.DbPassword), skipRowNum, skipDataNum, lineSeparator, textQualifier, columnSeparator);
                return Json(jsonResponse, "text/x-json");

            }
            catch (Exception ex)
            {
                jsonResponse.Success = false;
                jsonResponse.Message = ex.Message;
                return Json(jsonResponse, "text/x-json");
            }

        }
        private string[][] read_txt(string text, string columnSeparator, string textQualifier)
        {
            if (text == null)
            {
                return null;
            }
            var text_array = new List<string[]>();
            var line = new List<string>();
            var field = new StringBuilder();
            //是否在双引号内
            bool in_quata = false;
            //字段是否开始
            bool field_start = true;
            for (int i = 0; i < text.Length; i++)
            {
                string ch = text[i].ToString();
                if (in_quata)
                {
                    //如果已经处于双引号范围内
                    if (ch == textQualifier)
                    {
                        //如果是两个引号，则当成一个普通的引号处理
                        if (i < text.Length - 1 && text[i + 1].ToString() == textQualifier)
                        {
                            field.Append(textQualifier);
                            i++;
                        }
                        else
                        {
                            //否则退出引号范围
                            in_quata = false;
                        }
                    }
                    else //双引号范围内的任何字符（除了双引号）都当成普通字符
                    {
                        field.Append(ch);
                    }
                }
                else
                {
                    if (ch == columnSeparator)//新的字段开始
                    {
                        line.Add(field.ToString());
                        field.Remove(0, field.Length);
                        field_start = true;
                    }
                    else if (ch == textQualifier)//限定符的处理
                    {
                        if (field_start)
                        {
                            in_quata = true;
                        }
                        else
                        {
                            field.Append(ch);
                        }
                    }
                    else if (ch == "\r")//新的记录行开始
                    {
                        if (field.Length > 0 || field_start)
                        {
                            line.Add(field.ToString());
                            field.Remove(0, field.Length);
                        }
                        text_array.Add(line.ToArray());
                        line.Clear();
                        field_start = true;
                        //在 window 环境下，\r\n通常是成对出现，所以要跳过
                        if (i < text.Length - 1 && text[i + 1] == '\n')
                        {
                            i++;
                        }
                    }
                    else
                    {
                        field_start = false;
                        field.Append(ch);
                    }

                }
            }
            //文件结束
            if (field.Length > 0 || field_start)
            {
                line.Add(field.ToString());
            }
            if (line.Count > 0)
            {
                text_array.Add(line.ToArray());
            }
            return text_array.ToArray();
        }
        public string ReadTxtData(string connstr, string viewName, string DbUserID, string DbPassword, int skipRowNum, int skipDataNum, string lineSeparator, string textQualifier, string columnSeparator)
        {
            Stream stream = null;
            WebResponse response = null;
            if (connstr.StartsWith("ftp"))//取ftp文件流
            {
                FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(connstr + "/" + viewName));
                reqFTP.Credentials = new NetworkCredential(DbUserID, DbPassword);
                reqFTP.KeepAlive = false;
                reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
                response = reqFTP.GetResponse();
                stream = response.GetResponseStream();
            }
            else//取共享文件流
            {
                stream = System.IO.File.Open(connstr + "\\" + viewName, System.IO.FileMode.Open);
            }
            StringBuilder outputHtml = new StringBuilder();
            using (System.IO.StreamReader streamReader = new System.IO.StreamReader(stream, System.Text.Encoding.GetEncoding("gb2312")))
            {
                outputHtml.Append("<table id=\"tableSourceData\" class=\"pagTable cssnowrap\" cellspacing=\"0\" cellpadding=\"0\" ><tr class=\"pagTablehd\">");

                for (int i = 0; i < skipRowNum; i++)
                {
                    if (streamReader.Peek() >= 0)
                        streamReader.ReadLine();//跳过标题行
                }
                if (streamReader.Peek() >= 0)
                {
                    string[][] lineArr = read_txt(streamReader.ReadLine(), columnSeparator, textQualifier);
                    foreach (string[] col in lineArr)
                    {
                        foreach (string s in col)//标题列字段值
                        {
                            outputHtml.Append("<td>");
                            outputHtml.Append(s);
                            outputHtml.Append("&nbsp;</td>");
                        }
                        outputHtml.Append("\r\n");
                    }
                }
                for (int i = 0; i < skipDataNum; i++)
                {
                    if (streamReader.Peek() >= 0)
                        streamReader.ReadLine();//跳过数据行
                }

                for (int i = 0; i < 10; i++)
                {
                    if (streamReader.Peek() >= 0)
                    {
                        if (textQualifier == "<无>" || string.IsNullOrEmpty(textQualifier))
                            textQualifier = "";
                        string[][] lineArr = read_txt(streamReader.ReadLine(), columnSeparator, textQualifier);
                        foreach (string[] col in lineArr)
                        {
                            foreach (string s in col)//列字段值
                            {
                                outputHtml.Append("<td>");
                                outputHtml.Append(s);
                                outputHtml.Append("&nbsp;</td>");
                            }
                            outputHtml.Append("\r\n");
                        }
                    }
                }
            }
            try
            {
                if (response != null)
                    response.Close();
                stream.Close();
                stream.Dispose();
            }
            catch { }
            string html = outputHtml.ToString();
            switch (lineSeparator)
            {
                case "\\r\\n":
                    html = html.Replace("\r\n", "</tr><tr>");
                    break;
                case "\\r":
                    html = html.Replace("\r", "</tr><tr>");
                    break;
                case "\\n":
                    html = html.Replace("\n", "</tr><tr>");
                    break;
                case "\\t":
                    html = html.Replace("\t", "</tr><tr>");
                    break;
                default:
                    html.Replace(lineSeparator, "</tr><tr>");
                    break;
            }
            //if (textQualifier == "<无>" || string.IsNullOrEmpty(textQualifier))
            //{
            //    html = html.Replace(columnSeparator, "&nbsp;</td><td>");
            //}
            //else
            //{
            //    html = html.Replace(textQualifier + textQualifier + columnSeparator + textQualifier + textQualifier, "&nbsp;</td><td>").Replace(textQualifier + textQualifier, "");// 替换""value"",""value""中的 "",""
            //    html = html.Replace(textQualifier + columnSeparator + textQualifier, "&nbsp;</td><td>").Replace(textQualifier, "");// 替换 "value","value"中的 ","
            //    html = html.Replace(columnSeparator, "&nbsp;</td><td>"); //没有限定符
            //}
            html += "</tr></table>";

            return html;
        }

        public string SaveCsvFile(string systemID, string viewID, string dbName, FormCollection collection)
        {
            try
            {
                LoginInfo loginInfos = base.loginInfo();
                loginInfos.DBName = dbName;
                EtlSourceSystemBLL bll = new EtlSourceSystemBLL(loginInfos);
                EtlSourceViewConfigBLL sourceViewConfigBLL = new EtlSourceViewConfigBLL(loginInfos);
                var sourceViewConfigInfo = sourceViewConfigBLL.GetSourceViewConfigInfoByGuid(viewID);
                string dbType = Request.Params["dbType"];
                Dictionary<string, string> query = collection["query"].FromJSon<Dictionary<string, string>>();
                sourceViewConfigInfo.FileFormat = query["FileFormat"];
                if (dbType == "7")
                {
                    sourceViewConfigInfo.TextQualifier = "\"";
                    sourceViewConfigInfo.LineSeparator = "\\r\\n";
                    sourceViewConfigInfo.ColumnSeparator = ",";
                }
                else
                {
                    sourceViewConfigInfo.TextQualifier = query["TextQualifier"];
                    sourceViewConfigInfo.LineSeparator = query["LineSeparator"];
                    sourceViewConfigInfo.ColumnSeparator = query["ColumnSeparator"];
                }
                sourceViewConfigInfo.SkipRowNum = Convert.ToInt32(query["SkipRowNum"]);
                sourceViewConfigInfo.SkipDataNum = Convert.ToInt32(query["SkipDataNum"]);

                return sourceViewConfigBLL.UpdateSourceViewConfig(sourceViewConfigInfo).ToString();
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        #endregion

        #endregion

        #region 公共单元
        /// <summary>
        /// 检查初始化字段排序
        /// </summary>
        /// <returns></returns>
        public ActionResult CheckBigDataInit()
        {
            string tbModel = Request.Params["tableMode"];
            string paras = Request.Params["paras"];
            string type = Request.Params["type"];
            if (!string.IsNullOrEmpty(paras))
            {
                string cmpPara = string.Empty;
                List<int> paraArri = new List<int>();
                IEnumerable<string> paraArr = paras.Split('&').ToList();
                cmpPara = paraArr.ToJson();
                if (type == "int" || type == "float")
                {
                    try
                    {
                        foreach (string s in paraArr)
                        {
                            paraArri.Add(int.Parse(s));
                        }
                        cmpPara = paraArri.ToJson();
                    }
                    catch
                    {
                        return Json("添加字段值不符合指定类型。");
                    }
                }
                if (paraArr.Count() > 1)
                {
                    IEnumerable para = null;
                    if (tbModel == "2")
                    { //asc 
                        if (type == "int" || type == "float")
                        {
                            para = paraArri.OrderBy(o => { return o; });
                        }
                        else
                        {
                            para = paraArr.OrderBy(o => { return o; });
                        }
                    }
                    else
                    { //desc 
                        if (type == "int" || type == "float")
                        {
                            para = paraArri.OrderByDescending(o => { return o; });
                        }
                        else
                        {
                            para = paraArr.OrderByDescending(o => { return o; });
                        }
                    }
                    if (!cmpPara.Equals(para.ToJson()))
                        return Json("0");
                }
            }
            return Json("1");
        }
        public ActionResult GetPathData(string dbName)
        {
            JsonResponse<string> jsonResponse = new JsonResponse<string>();
            Dictionary<String, Object> jsonDict = new Dictionary<String, Object>();
            try
            {
                string order = Request.Params["order"];
                string sort = Request.Params["sort"];
                string id = Request.Params["id"];
                string fromType = Request.Params["fromType"];
                string getAll = Request.Params["getAll"];

                if (string.IsNullOrEmpty(sort))
                    sort = "desc";
                if (string.IsNullOrEmpty(order))
                    order = "";

                LoginInfo loginInfos = base.loginInfo();
                loginInfos.DBName = dbName;
                EtlSourceSystemBLL sourceSystemBLL = new EtlSourceSystemBLL(loginInfos);
                EtlSourceViewConfigBLL sourceViewConfigBLL = new EtlSourceViewConfigBLL(loginInfos);
                DataTable dt = new DataTable();

                List<string> ids = new List<string>();
                switch (fromType)
                {
                    case "source":
                        if (getAll == "1") //系统下所有记录
                        {
                            List<ETL_Source_ViewConfigInfo> list = sourceSystemBLL.GetSourceViewConfigBySystemID(id);
                            foreach (ETL_Source_ViewConfigInfo model in list)
                            {
                                ids.Add(model.Guid);
                            }
                        }
                        else
                        {
                            ids.Add(id);
                        }
                        SqlParameter[] para = new SqlParameter[] { 
                            new SqlParameter("@ids",string.Join(",",ids))
                            };
                        dt = sourceSystemBLL.GetPathList("select 源系统名称,远端源表名称,源表显示名称,模型显示名称,对接系统名称,推送名称,推送组名称 from Working_db.GetPathFromSource(@ids)", para);
                        break;
                    case "target":
                        ETL_Target_ViewConfigBLL targetSystemBLL = new ETL_Target_ViewConfigBLL(loginInfos);
                        if (getAll == "1") //系统下所有记录
                        {
                            List<ETL_Target_ViewConfigInfo> tarList = targetSystemBLL.GetViewConfigById(id);
                            foreach (ETL_Target_ViewConfigInfo model in tarList)
                            {
                                ids.Add(model.Guid);
                            }
                        }
                        else
                        {
                            ids.Add(id);
                        }
                        SqlParameter[] tarpara = new SqlParameter[] { 
                            new SqlParameter("@ids",string.Join(",",ids))
                            };
                        dt = sourceSystemBLL.GetPathList("select 推送组名称,推送名称,对接系统名称,模型显示名称,源系统名称,远端源表名称,源表显示名称 from Working_db.GetPathFromTarget(@ids)", tarpara);
                        break;
                    case "model":
                        SqlParameter[] modelpara = new SqlParameter[] { 
                            new SqlParameter("@ids",id)
                            };
                        dt = sourceSystemBLL.GetPathList("select 推送组名称,推送名称,对接系统名称,模型显示名称,源系统名称,远端源表名称,源表显示名称 from Working_db.GetPathFromModel(@ids)", modelpara);
                        break;
                }


                jsonResponse.Data = this.DataTableToString(dt, 0, order, sort, false);
                jsonResponse.Head = GetSourceTableHeadData(dt.Columns);
                jsonResponse.Success = true;
                jsonResponse.Message = "OK";
                return Json(jsonResponse);
            }
            catch (Exception ex)
            {
                jsonResponse.Success = false;
                jsonResponse.Message = "信息获取失败！" + ex.Message;
                jsonResponse.Data = "";
                return Json(jsonResponse);
            }
        }
        /// <summary>
        /// 数据库类型列表
        /// </summary>
        /// <param name="keyName"></param>
        /// <param name="dbName"></param>
        /// <returns></returns>
        public ActionResult BindDropwown(string keyName, string dbName)
        {
            LoginInfo loginInfos = base.loginInfo();
            DictionaryBLL bll = new DictionaryBLL(loginInfos);
            return Json(bll.GetListByKeyName(keyName));
        }
        public ActionResult GetDBList()
        {
            return View();
        }

        /// <summary>
        /// 测试数据库连接
        /// </summary>
        /// <param name="dbType"></param>
        /// <param name="connectionString"></param>
        /// <returns></returns>
        public ActionResult TestConeciton(string dbType, string connectionString)
        {
            Dictionary<string, string> query = connectionString.FromJSon<Dictionary<string, string>>();

            string strCon = "";
            string dataSource = query["Host"];
            string database = query["Server"];
            string userID = query["User"];
            string password = query["Pass"];
            string port = query["Part"];
            string sid = query["SID"];
            string fileUrl = query["FileUrl"];
            string fileUser = query["FileUser"];
            string filePass = query["FilePass"];
            //try
            //{
                LoginInfo loginInfos = base.loginInfo();
                SysPublicBLL bll = new SysPublicBLL(loginInfos);
                string conn = "";
                switch (dbType)
                {
                    case "1":
                        strCon = string.Format(SQLSERVER_FRAMAT, dataSource, database, userID, password, port, sid);
                        conn = bll.TestConeciton(int.Parse(dbType), strCon);
                        break;
                    case "2":
                        strCon = string.Format(ORACLE_FRAMAT, dataSource, database, userID, password, port, sid);
                        if (dataSource.IndexOf("|") > -1)
                        {
                            List<string> hostArr = dataSource.Split('|').ToList();
                            List<string> portArr = port.Split('|').ToList();
                            StringBuilder ora = new StringBuilder();
                            ora.Append("Data Source =(DESCRIPTION =");
                            for (int i = 0; i < hostArr.Count; i++)
                            {
                                ora.Append(string.Format(" (ADDRESS = (PROTOCOL = TCP)(HOST = {0})(PORT = {1}))", hostArr[i], portArr[i]));
                            }
                            ora.Append(string.Format("(CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = {0})));User Id={1};Password={2};", database, userID, password));
                            strCon = ora.ToString();
                        }
                        conn = bll.TestConeciton(int.Parse(dbType), strCon);
                        break;
                    case "4":
                    case "8":
                        strCon = string.Format(MYSQL_FRAMAT, dataSource, database, userID, password, port, sid);
                        conn = bll.TestConeciton(int.Parse(dbType), strCon);
                        break;
                    default:
                        strCon = string.Format(ACCESS_FRAMAT, fileUrl, fileUser, filePass);
                        if (fileUrl.StartsWith("ftp", StringComparison.CurrentCultureIgnoreCase))
                        {
                            List<string> a = new List<string>();
                            conn = SysPublicBLL.readerFtp(fileUrl, fileUser, filePass, out a);
                        }
                        else
                            conn = SetFileShared(strCon, fileUser, filePass);
                        break;
                }
                return Json(conn);
            //}
            //catch (Exception ex)
            //{
            //    return Json(ex.Message);
            //}
        }
        /// <summary>
        /// 拷贝ftp地址下的Excel和Access数据库
        /// </summary>
        public void CopyFTPFile()
        {
            string sourceID = Request.Params["sourceID"];
            string ftpUrl = Request.Params["ftpUrl"].Trim();
            string fileUser = Request.Params["fileUser"].Trim();
            string filePass = Request.Params["filePass"].Trim();
            string wd = Request.Params["wd"].Trim();
            string extList = Request.Params["extList"].Trim();
            string folder = HttpContext.Server.MapPath("/files/" + sourceID.Replace("-", ""));

            if (!System.IO.Directory.Exists(folder))//创建本地文件夹
                System.IO.Directory.CreateDirectory(folder);
            if (ftpUrl.StartsWith("ftp", StringComparison.CurrentCultureIgnoreCase))
            {
                List<string> dirList = new List<string>();
                SysPublicBLL.readerFtp(ftpUrl, fileUser, filePass, out dirList);//获取ftp文件名列表
                List<string> wdArr = wd.Split('$').ToList<string>();
                List<string> extArr = extList.Split('$').ToList<string>();
                foreach (string fname in dirList)
                {
                    //拷贝ftp地址下的Excel和Access数据库 
                    if (extArr.FindIndex(o => { return fname.ToLower().IndexOf(o) > 0; }) > -1 && (string.IsNullOrEmpty(wd) || wdArr.FindIndex(o => { return fname.ToLower().IndexOf(o.ToLower()) > -1; }) > -1))
                        SysPublicBLL.DownloadFtpFile(ftpUrl + "/" + fname, fileUser, filePass, folder + "\\" + fname);
                }
            }
        }
        /// <summary>
        /// 设置文件共享
        /// </summary>
        /// <param name="path">网络路径</param>
        /// <param name="userName">用户名</param>
        /// <param name="passWord">密码</param>
        /// <returns>true false</returns>
        private string SetFileShared(string f, string userName, string passWord)
        {
            string dosLine = string.Format("net use {0} \"{2}\" /user:\"{1}\" /PERSISTENT:YES", f.Substring(0, f.LastIndexOf("\\")), userName, passWord);
            return RunDos(dosLine);
        }
        /// <summary>
        /// 执行dos命令
        /// </summary>
        /// <param name="dosLine"></param>
        /// <returns></returns>
        private string RunDos(string dosLine)
        {
            string msg = "";
            Process proc = new Process();

            try
            {
                proc.StartInfo.FileName = "cmd.exe";
                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.RedirectStandardInput = true;
                proc.StartInfo.RedirectStandardOutput = true;
                proc.StartInfo.RedirectStandardError = true;
                proc.StartInfo.CreateNoWindow = true;
                proc.Start();
                proc.StandardInput.WriteLine(dosLine);
                proc.StandardInput.WriteLine("exit");
                while (!proc.HasExited)
                {
                    proc.WaitForExit(1000);
                }
                string errormsg = proc.StandardError.ReadToEnd();
                proc.StandardError.Close();
                if (string.IsNullOrEmpty(errormsg))
                {
                    msg = "1";
                }
                else
                    msg = errormsg;

            }
            catch (Exception ex)
            {
                msg = ex.Message;
            }
            finally
            {
                proc.Close();
                proc.Dispose();
            }
            return msg;
        }
        /// <summary>
        /// 设置数据库表字段类型
        /// </summary>
        /// <param name="columnType"></param>
        /// <param name="columnLength"></param>
        /// <param name="columnPrecision"></param>
        /// <param name="decimalPlaces"></param>
        /// <param name="DBType"></param>
        /// <returns></returns>

        public bool CheckType(string typename, string columnName, string dbName)
        {
            LoginInfo loginInfos = base.loginInfo();
            loginInfos.DBName = dbName;
            SysPublicBLL bll = new SysPublicBLL(loginInfos);
            return bll.CheckType(typename, columnName);
        }

        /// <summary>
        /// 解析数据库字符串
        /// </summary>
        /// <param name="collection"></param>
        /// <returns></returns>
        private List<ETLDbConfig> GetDatabaseConnections(FormCollection collection)//, int sourceType)
        {
            List<ETLDbConfig> list = new List<ETLDbConfig>();
            var sourceType = Convert.ToInt32(collection["SourceType"]);

            JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
            IList<ETLDbInfo> jsonList = (IList<ETLDbInfo>)jsSerializer.Deserialize(collection["DBs"], typeof(IList<ETLDbInfo>));
            foreach (ETLDbInfo dbInfo in jsonList)
            {
                string dataSource = dbInfo.Host;
                string database = dbInfo.Server;
                string userID = dbInfo.User;
                string password = dbInfo.Pass;
                string port = dbInfo.Part;
                string sid = dbInfo.SID;
                string wildCard = dbInfo.WildCard;
                int extractOrder = dbInfo.ExtractOrder;
                string strCon = string.Empty;

                switch (sourceType)
                {
                    case 1:
                        strCon = string.Format(SQLSERVER_FRAMAT, dataSource, database, userID, password, port, sid);
                        break;
                    case 2:
                        strCon = string.Format(ORACLE_FRAMAT, dataSource, database, userID, password, port, sid);
                        if (dataSource.IndexOf("|") > -1)
                        {
                            List<string> hostArr = dataSource.Split('|').ToList();
                            List<string> portArr = dbInfo.Part.Split('|').ToList();
                            StringBuilder ora = new StringBuilder();
                            ora.Append("Data Source=(DESCRIPTION=");
                            for (int i = 0; i < hostArr.Count; i++)
                            {
                                ora.Append(string.Format("(ADDRESS = (PROTOCOL = TCP)(HOST = {0})(PORT = {1}))", hostArr[i], portArr[i]));
                            }
                            ora.Append(string.Format("(CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = {0})));User Id={1};Password={2};", database, userID, password));
                            strCon = ora.ToString();
                        }
                        break;
                    case 4:
                    case 8:
                        strCon = string.Format(MYSQL_FRAMAT, dataSource, database, userID, password, port, sid);
                        break;
                    case 3:
                    case 5:
                    case 6:
                    case 7:
                        dataSource = dbInfo.FileUrl;
                        userID = dbInfo.FileUser;
                        password = dbInfo.FilePass;
                        strCon = string.Format(ACCESS_FRAMAT, dbInfo.FileUrl, dbInfo.FileUser, dbInfo.FilePass);
                        break;
                }
                list.Add(new ETLDbConfig()
                {
                    GUID = dbInfo.GUID,
                    DbDataSource = dataSource,
                    DbDatabase = database,
                    DbUserID = userID,
                    DbPassword = encrypCode.Encrypto(password),
                    DbPort = port,
                    DbSID = sid,
                    WildCard = wildCard,
                    ExtractOrder = extractOrder,
                    DbConnectionStr = encrypCode.Encrypto(strCon),
                    Description = dbInfo.Description
                });
            }
            return list;
        }

        /// <summary>
        /// 测试数据库连接
        /// </summary>
        /// <param name="dbType"></param>
        /// <param name="collection"></param>
        /// <returns></returns>
        private List<ETLDbConfig> GetTestDatabaseConnections(int dbType, FormCollection collection)//, int sourceType)
        {
            List<ETLDbConfig> list = new List<ETLDbConfig>();
            // dbType=1&connectionString[0][DbDataSource]=111&connectionString[0][DbDatabase]=&connectionString[0][DbUserID]=&connectionString[0][DbPassword]=&connectionString[0][DbPort]=&connectionString[0][DbSID]=&connectionString[1][DbDataSource]=222&connectionString[1][DbDatabase]=&connectionString[1][DbUserID]=&connectionString[1][DbPassword]=&connectionString[1][DbPort]=&connectionString[1][DbSID]=
            int i = 0;
            while (collection[string.Format("connectionString[{0}][DbDataSource]", i)] != null)
            {
                var dataSource = collection[string.Format("connectionString[{0}][DbDataSource]", i)];
                var database = collection[string.Format("connectionString[{0}][DbDatabase]", i)] != null ? collection[string.Format("connectionString[{0}][DbDatabase]", i)] : string.Empty;
                var userID = collection[string.Format("connectionString[{0}][DbUserID]", i)] != null ? collection[string.Format("connectionString[{0}][DbUserID]", i)] : string.Empty;
                var password = collection[string.Format("connectionString[{0}][DbPassword]", i)] != null ? collection[string.Format("connectionString[{0}][DbPassword]", i)] : string.Empty;
                var port = collection[string.Format("connectionString[{0}][DbPort]", i)] != null ? collection[string.Format("connectionString[{0}][DbPort]", i)] : "-1";
                var sid = collection[string.Format("connectionString[{0}][DbSID]", i)] != null ? collection[string.Format("connectionString[{0}][DbSID]", i)] : string.Empty;
                var strCon = string.Empty;
                if (string.IsNullOrEmpty(port))
                {
                    port = "";
                }
                switch (dbType)
                {
                    case 1:
                        strCon = string.Format(SQLSERVER_FRAMAT, dataSource, database, userID, password, port, sid);
                        break;
                    case 2:
                        strCon = string.Format(ORACLE_FRAMAT, dataSource, database, userID, password, port, sid);
                        break;
                    case 3:
                        strCon = string.Format(ACCESS_FRAMAT, dataSource, database, userID, password, port, sid);
                        break;
                    case 4:
                        strCon = string.Format(MYSQL_FRAMAT, dataSource, database, userID, password, port, sid);
                        break;
                    case 5:
                        strCon = string.Format(EXCEL_FRAMAT, dataSource, database, userID, password, port, sid);
                        break;
                    case 6:
                        strCon = string.Format(TXT_FRAMAT, dataSource, database, userID, password, port, sid);
                        break;
                    default:
                        strCon = string.Format(SQLSERVER_FRAMAT, dataSource, database, userID, password, port, sid);
                        break;
                }
                list.Add(new ETLDbConfig()
                {
                    DbDataSource = dataSource,
                    DbDatabase = database,
                    DbUserID = userID,
                    DbPassword = password,
                    DbPort = port,
                    DbSID = sid,
                    DbConnectionStr = strCon
                });
                i++;
            }
            return list;
        }

        private List<ETL_Source_ViewConfigInfo> GetSourceViewConfigs(FormCollection collection)
        {
            JavaScriptSerializer java = new JavaScriptSerializer();

            List<ETL_Source_ViewConfigInfo> list = new List<ETL_Source_ViewConfigInfo>();
            int i = 0;
            while (collection[string.Format("SourceViewConfig[{0}][Guid]", i)] != null)
            {
                var guid = collection[string.Format("SourceViewConfig[{0}][Guid]", i)];
                var viewName = collection[string.Format("SourceViewConfig[{0}][ViewName]", i)] != null ? collection[string.Format("SourceViewConfig[{0}][ViewName]", i)] : string.Empty;
                var viewChnName = collection[string.Format("SourceViewConfig[{0}][ViewChnName]", i)] != null ? collection[string.Format("SourceViewConfig[{0}][ViewChnName]", i)] : string.Empty;
                var DBID = collection[string.Format("SourceViewConfig[{0}][DBID]", i)] != null ? collection[string.Format("SourceViewConfig[{0}][DBID]", i)] : string.Empty;

                list.Add(new ETL_Source_ViewConfigInfo()
                {
                    Guid = guid,
                    ViewName = viewName,
                    ViewChnName = viewChnName,
                    DBID = DBID
                });
                i++;
            }
            return list;
        }

        /// <summary>
        /// sortBy : 1 小到大
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="sortField"></param>
        /// <param name="sortBy"></param>
        /// <returns></returns>
        public string DataTableToString(DataTable dt, int width, string sortField, string sortBy, bool click)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("");
            sb.AppendLine(string.Format("<table id=\"tbSoutceTableData\" class=\"pagTableNofixed\" cellspacing=\"0\" cellpadding=\"0\" style=\"{0}\">", (width > 0 ? string.Format("width: {0}px;", width) : string.Empty)));
            sb.AppendLine("<tbody>");
            sb.AppendLine(" <tr class=\"pagTablehd\">");
            int columnWidth = 100 / dt.Columns.Count;
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                string textalign = "";
                if (dt.Columns[i].DataType.Equals(typeof(int))
                    || dt.Columns[i].DataType.Equals(typeof(float))
                    || dt.Columns[i].DataType.Equals(typeof(DateTime)))
                {
                    textalign = "center";
                }
                else
                {
                    textalign = "left";
                }
                string columnName = dt.Columns[i].ColumnName;
                string beClick = "";
                if (click)
                    beClick = string.Format("onclick=\"initSourceTableData('{0}','{1}')\"", columnName
                    , sortField.Equals(columnName, StringComparison.CurrentCultureIgnoreCase) ? (sortBy.Equals("desc") ? "asc" : "desc") : "desc");
                sb.AppendLine(string.Format("<td class=\"DataGrid2ViewName csscursor cssnowrap\" style=\"width: {0}%; text-align: {1};\" {2} >"
                    , columnWidth, textalign, beClick));
                sb.AppendLine(string.Format("{1}{0}", columnName
                    , sortField.Equals(columnName, StringComparison.CurrentCultureIgnoreCase) ? (sortBy.Equals("desc") ? "<img src='/images/datagrid_sort_asc.gif'>" : "<img src='/images/datagrid_sort_desc.gif'>") : ""
                    ));
                sb.AppendLine("</td>");
            }
            sb.AppendLine(" </tr>");
            int rowNumber = 0;
            foreach (DataRow rowItem in dt.Rows)
            {
                if (rowNumber++ % 2 == 0)
                {
                    sb.AppendLine(" <tr>");
                }
                else
                {
                    sb.AppendLine(" <tr class=\"tr_c2\">");
                }
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    string textalign = "";
                    if (dt.Columns[i].DataType.Equals(typeof(int))
                        || dt.Columns[i].DataType.Equals(typeof(float))
                        || dt.Columns[i].DataType.Equals(typeof(DateTime)))
                    {
                        textalign = "center";
                    }
                    else
                    {
                        textalign = "left";
                    }

                    var value = rowItem[i] == null || string.IsNullOrEmpty(rowItem[i].ToString()) ? "&nbsp;" : rowItem[i].ToString();
                    int vlength = value.Length;
                    if (vlength > 200)
                        value = value.Substring(0, 200) + "...";
                    var title = value.Equals("&nbsp;") ? "" : string.Format("内容：{0}\n长度：{1}", value, vlength);
                    sb.AppendLine(string.Format("     <td style=\"text-align: {0};\" title=\"{1}\">", textalign, title));
                    //if (dt.Columns[i].DataType.Equals(typeof(DateTime)))
                    {
                        sb.AppendLine("     <nobr>");
                    }
                    if (value.Length > 50)
                        value = value.Substring(0, 50) + "...";
                    sb.AppendLine(string.Format("         {0}", value));
                    //if (dt.Columns[i].DataType.Equals(typeof(DateTime)))
                    {
                        sb.AppendLine("     </nobr>");
                    }
                    sb.AppendLine("     </td>");
                }
                sb.AppendLine(" </tr>");
            }
            sb.AppendLine("</tbody>");
            sb.AppendLine("</table>");
            return sb.ToString();
        }

        private string DataTableToString(DataTable dt, Dictionary<string, string> columnNameDics, string sortField, string sortBy, string onColumnClick)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("");
            sb.AppendLine("<table id=\"tbSoutceTableData\" class=\"pagTableNofixed\" cellspacing=\"0\" cellpadding=\"0\">");
            sb.AppendLine("<tbody>");
            sb.AppendLine(" <tr class=\"pagTablehd\">");
            int width = 100 / dt.Columns.Count;
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                string textalign = "";
                if (dt.Columns[i].DataType.Equals(typeof(int))
                    || dt.Columns[i].DataType.Equals(typeof(float))
                    || dt.Columns[i].DataType.Equals(typeof(DateTime)))
                {
                    textalign = "center";
                }
                else
                {
                    textalign = "left";
                }
                string columnName = dt.Columns[i].ColumnName;
                string columnCaption = dt.Columns[i].ColumnName;
                if (columnNameDics != null && columnNameDics.ContainsKey(columnName.ToUpper()))
                {
                    if (!string.IsNullOrEmpty(columnNameDics[columnName.ToUpper()]))
                    {
                        columnCaption = columnNameDics[columnName.ToUpper()];
                    }
                }
                sb.AppendLine(string.Format("     <td class=\"DataGrid2ViewName csscursor\" style=\"width: {0}%; text-align: {1};\" title='{3}' onclick=\"{5}('{2}',{4})\">"
                    , width, textalign
                    , columnName
                    , columnCaption
                    , sortField.Equals(columnName, StringComparison.CurrentCultureIgnoreCase) ? (sortBy.Equals("1") ? "0" : "1") : "1"
                    , !string.IsNullOrEmpty(onColumnClick) ? onColumnClick : "initSourceTableData"
                    ));


                sb.AppendLine(string.Format("         {1}{0}", columnName
                    , sortField.Equals(columnName, StringComparison.CurrentCultureIgnoreCase) ? (sortBy.Equals("1") ? "<img src='/images/datagrid_sort_asc.gif'>" : "<img src='/images/datagrid_sort_desc.gif'>") : ""
                    ));
                sb.AppendLine("     </td>");
            }
            sb.AppendLine(" </tr>");
            int rowNumber = 0;
            foreach (DataRow rowItem in dt.Rows)
            {
                if (rowNumber++ % 2 == 0)
                {
                    sb.AppendLine(" <tr>");
                }
                else
                {
                    sb.AppendLine(" <tr class=\"tr_c2\">");
                }
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    string textalign = "";
                    if (dt.Columns[i].DataType.Equals(typeof(int))
                        || dt.Columns[i].DataType.Equals(typeof(float))
                        || dt.Columns[i].DataType.Equals(typeof(DateTime)))
                    {
                        textalign = "center";
                    }
                    else
                    {
                        textalign = "left";
                    }

                    var value = rowItem[i] == null || string.IsNullOrEmpty(rowItem[i].ToString()) ? "&nbsp;" : rowItem[i].ToString();
                    int vlength = value.Length;
                    if (vlength > 200)
                        value = value.Substring(0, 200) + "...";
                    var title = value.Equals("&nbsp;") ? "" : string.Format("内容：{0}\n长度：{1}", value, vlength);
                    sb.AppendLine(string.Format("     <td style=\"text-align: {0};\" title=\"{1}\">", textalign, title));
                    //if (dt.Columns[i].DataType.Equals(typeof(DateTime)))
                    {
                        sb.AppendLine("     <nobr>");
                    }
                    if (vlength > 50)
                        value = value.Substring(0, 50) + "...";
                    sb.AppendLine(string.Format("         {0}", value));
                    //if (dt.Columns[i].DataType.Equals(typeof(DateTime)))
                    {
                        sb.AppendLine("     </nobr>");
                    }
                    sb.AppendLine("     </td>");
                }
                sb.AppendLine(" </tr>");
            }
            sb.AppendLine("</tbody>");
            sb.AppendLine("</table>");
            return sb.ToString();
        }
        /// <summary>
        /// 取目录下文件列表
        /// </summary>
        /// <param name="path"></param>
        /// <param name="ext"></param>
        /// <returns></returns>
        public void GetFileNames(string path, string ext, out List<string> outfileList)
        {
            outfileList = new List<string>();
            if (Directory.Exists(path))
            {
                var fileNames = Directory.GetFiles(path);
                Dictionary<string, string> fileDict = new Dictionary<string, string>();

                foreach (string fullfileName in fileNames)
                {
                    var fileName = System.IO.Path.GetFileName(fullfileName);
                    fileName = System.Text.RegularExpressions.Regex.Replace(fileName, "_([0-9]{8})\\.", ".");
                    var fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(fileName).ToUpper();
                    var extension = System.IO.Path.GetExtension(fullfileName);
                    if (extension == ext)
                    {
                        if (!fileDict.ContainsKey(fileNameWithoutExtension))
                        {
                            fileDict.Add(fileNameWithoutExtension, fileName);
                        }
                    }
                }
                foreach (var fileName in fileDict.Values)
                {
                    outfileList.Add(fileName);
                }
            }
        }
         
        #endregion
    }
    #region 视图模型类

    public class ViewTemp
    {
        public string ID { get; set; }
        public string SchemaName { get; set; }
        public string SchemaCnName { get; set; }
        public string TableName { get; set; }
        private string _ChnName = string.Empty;
        public string ChnName
        {
            get
            {
                if (string.IsNullOrEmpty(_ChnName))
                {
                    return "";
                }
                return _ChnName;
            }
            set
            {
                _ChnName = value;
            }
        }
        public string Modelid { get; set; }
        public int IsConfigured { get; set; }
    }

    public class ETL_Target_View_ColumnConfigTrans
    {
        public string ColumnName { get; set; }
        public string ColumnChnName { get; set; }
        public string ColumnOrderType_OrderBy { get; set; }
        public int ColumnOrderID_OrderBy { get; set; }
        public string ColumnAggregate { get; set; }
        public int ColumnIsGroup { get; set; }
        public string FixedValue { get; set; }
        public string ColumnOperator { get; set; }
        public string ColumnValue { get; set; }
        public int IsSaved { get; set; }
        public int column_id { get; set; }
    }

    public class MDPColumnTrans
    {
        public int column_id { get; set; }
        public string name { get; set; }
    }

    #endregion

}
